Page 79 - DCAP312_WEB_TECHNOLOGIES_II
P. 79
Unit 4: Advanced Server Controls
<asp:RangeValidator id= “RangeValidator1” runat= “server” Notes
ControlToValidate= “TextBox1”
ErrorMessage= “Your last name needs to be between M and P”
MaximumValue= “Q” MinimumValue= “M”></asp:RangeValidator>
The Regular Expression Validator Control
The Regular Expression Validator server control is a corroboration control that enables us to
confirm the user’s input based on a prototype defined by a normal expression. This is a great
control to check whether the user has entered a valid e-mail address or telephone number. In the
past, these kinds of validations took a considerable amount of JavaScript coding. The Regular
Expression Validator control with ASP.NET saves coding time.
In the Properties window for the Regular Expression Validator server control, click the button
in the Validation Expression box, and Visual Studio.NET provides we with a short list of
expressions to use in our form via the Regular Expression Editor. However, we are not limited
to these regular expressions in our ASP.NET applications. The list of prepared expressions is
shown in Figure 4.3.
Validating an e-mail address
Email:
<asp:TextBox id= “TextBox1” runat= “server”></asp:TextBox>
<asp:RegularExpressionValidator id= “RegularExpressionValidator1”
runat= “server” ControlToValidate= “TextBox1”
ErrorMessage= “You must enter an email address”
ValidationExpression= “\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*”>
</asp:RegularExpressionValidator>
In this example, notice that we place the Internet e-mail addresses regular
expression in our Validation Expression property. The great thing is that it
is pretty simple, and it takes hardly any coding. Figure 4.5 shows the error
message that results if a user enters an incorrect e-mail address in the text
box.
Figure 4.4: The regular expression editor
LOVELY PROFESSIONAL UNIVERSITY 73