- RegularExpressionValidator Web Server Control
in ASP.NET environment,This article describes you how to work with RegularExpressionValidator Web Control.It is an another Validation Contorl which can be performed an Operation as Checks that the entry matches a pattern defined by a regular expression.
This type of validation enables you to check for predictable Expressions, such as in e-mail addresses, telephone numbers, postal codes, and so on. The regular-expression validation implementation is different on the client than on the serve.
The following code snippets explains you the RegularExpressionValidator control
-
<asp:RegularExpressionValidator ID="Rev1" ControlToValidate="txt1"
ValidationExpression="\d{7}"
Display="Static" ErrorMessage="Zip code must be 7 numeric digits"
EnableClientScript="False"
runat="server" /<
-
- Formating the RegularExpressionValidator
ASP.NET validation controls allows you to customizing the format—font, size, and so on—of error text, or you can substitute a marker for error text. For example, you can have the validation control display an asterisk (*) when an error occurs.
You can also include a detailed error message in the ErrorMessage property of the validation control and add a ValidationSummary control to the page. The detailed ErrorMessage property text will appear on the page in the location of the ValidationSummary control.
To format error messages, specify the following properties,
- ForeColor - The color of the error message text.
- BackColor -The color behind the text.
- Font -The font face, size, weight, and so on.
- BorderWidth, BorderColor, and BorderStyle -The size and color of a border around the error message.
The following example demonstrates how to use the RequiredFieldValidator control to make sure that the user enters a value into the text box.
-
in .aspx.cs page,
-
void ButtonClick(Object sender, EventArgs e)
{
if (Page.IsValid)
{
Label1.Text="valid page";
}
else
{
Label1.Text="not valid page!";
}
}
-
in .aspx page,
-
<form id="Form2" runat="server">
<table bgcolor="#eeeeee" cellpadding="10">
<tr valign="top">
<td colspan="3">
<asp:Label ID="lblOutput" Text="Enter a 7 digit zip code"
runat="server" />
</td>
</tr>
<tr>
<td align="right">
Zip Code:
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" />
</td>
<td>
<asp:RegularExpressionValidator ID="rev1" ControlToValidate="txt1"
ValidationExpression="\d{7}" Display="Static"
ErrorMessage="Zip code must be 7 numeric digits"
EnableClientScript="False" runat="server" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" Text="Validate"
OnClick="ValidateBtn_Click" runat="server" />
</td>
<td>
</td>
</tr>
</table>
</form>
-
- Related Links
-
ea65d4f0-9ac0-4e23-ad86-69cd588301d1|0|.0