Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng]-1.pdf
Скачиваний:
143
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

CustomValidator

Regular Expressions in JavaScript4

another great article, this time on the use of regular expressions with JavaScript

Table 6.2. Common regular expression components and their descriptions

Special

Description

Character

 

.

any character

^

beginning of string

$

end of string

\d

numeric digit

\s

whitespace character

\S

non-whitespace character

(abc)

the string abc as a group of characters

?

preceding character or group is optional

+

one or more of the preceding character or group

*

zero or more of the preceding character or group

{n}

exactly n of the preceding character or group

{n,m}

n to m of the preceding character or group

(a|b)

either a or b

\$

a dollar sign (as opposed to the end of a string); we can ‘escape’

 

any of the special characters listed above by preceding it with

 

a backslash. For example, \. matches a period character, \?

 

matches a question mark, and so on

You’ll find a complete guide and reference to regular expressions and their components in the .NET Framework SDK Documentation.

CustomValidator

The validation controls included with ASP.NET allow you to handle many kinds of validation, yet certain types of validation cannot be performed with these built-in controls. For instance, imagine that you needed to ensure that a new

4 http://www.sitepoint.com/article/expressions-javascript

239

Chapter 6: Using the Validation Controls

user’s login details were unique by checking them against a list of existing usernames on the server. The CustomValidator control can be helpful in this situation, and others like it. Let’s see how:

Visual Basic File: CustomValidator.aspx (excerpt)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<title>CustomValidator Control Sample</title> <script runat="server" language="VB">

Sub CheckUniqueUserName(s As Object, _ e As ServerValidateEventArgs)

Dim username As String = e.Value.ToLower

If (username = "zak" Or username = "cristian") Then e.IsValid = False

End If End Sub

Sub submitButton_Click(s As Object, e As EventArgs) If Page.IsValid Then

submitButton.Text = "Valid" Else

submitButton.Text = "Invalid!" End If

End Sub </script>

</head>

<body>

<form runat="server"> <p>

New Username:<br />

<asp:TextBox ID="usernameTextBox" runat="server" /> <asp:CustomValidator ID="usernameUnique" runat="server"

ControlToValidate="usernameTextBox"

OnServerValidate="CheckUniqueUserName" ErrorMessage="This username already taken!" />

</p>

<p>

<asp:Button ID="submitButton" runat="server" OnClick="submitButton_Click" Text="Submit" />

</p>

</form>

</body>

</html>

240

CustomValidator

C#

File: CustomValidator.aspx (excerpt)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<title>CustomValidator Control Sample</title> <script runat="server" language="C#">

void CheckUniqueUserName(Object s, ServerValidateEventArgs e)

{

string username = e.Value.ToLower(); if(username == "zak" || username == "cristian")

{

e.IsValid = false;

}

}

void submitButton_Click(Object s, EventArgs e)

{

if(Page.IsValid)

{

submitButton.Text = "Valid";

}

else

{

submitButton.Text = "Invalid!";

}

}

</script>

</head>

<body>

<form runat="server"> <p>

New Username:<br />

<asp:TextBox ID="usernameTextBox" runat="server" /> <asp:CustomValidator ID="usernameUnique" runat="server"

ControlToValidate="usernameTextBox"

OnServerValidate="CheckUniqueUserName" ErrorMessage="This username already taken!" />

</p>

<p>

<asp:Button ID="submitButton" runat="server" OnClick="submitButton_Click" Text="Submit" />

</p>

</form>

241