Wednesday 5 March 2014

RegularExpressionValidator in ASP.NET

In this article I will explain what RegularExpressionValidator in ASP.NET is and what are the syntax and properties.

The RegularExpressionValidator control is used to ensure that an input value matches a specified pattern. . This Validator control is ASP.NET control.

If you capture input by using server controls, you can use the RegularExpressionValidator control to validate that input. You can use regular expressions to restrict the range of valid characters, to strip unwanted characters, and to perform length and format checks. You can constrain the input format by defining patterns that the input must match.

Syntax:

< RegularExpressionValidator />


Properties:

o   ControlToValidate:  The id of the control to validate
o   Display: The display behavior for the validation control. Legal values are:
§  None (the control is not displayed. Used to show the error message only in the ValidationSummary control)
§  Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation.
§  Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation
o   ErrorMessage: The text to display in the ValidationSummary control when validation fails. Note: This text will also be displayed in the validation control if the Text property is not set
o   Id: A unique id for the control
o   Runat: Specifies that the control is a server control. Must be set to "server"
o   ValidationExpression: Specifies the expression used to validate input control. The expression validation syntax is different on the client than on the server. JScript is used on the client. On the server, the language you have specified is used

Example 1: RegularExpression for EmailID

<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEmail" runat="server"
    ErrorMessage="*" ControlToValidate="txtEmail"
    ValidationGroup="GROUP" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2"
    runat="server" ErrorMessage="Please Enter Valid Email ID"
    ValidationGroup=" GROUP " ControlToValidate="txtEmail"
    CssClass="requiredFieldValidateStyle"
    ForeColor="Red"
    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>

Example 1: RegularExpression for U S Phone number ((000-000-0000))

<asp:TextBox ID="txtUSPhoneNumber" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvPhoneNumber" runat="server"
                ErrorMessage="*" ControlToValidate="txtUSPhoneNumber"
                    ValidationGroup="GROUP" ForeColor="Red"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1"
                runat="server" ErrorMessage="Phone Number is not valid"
                    ValidationGroup="GROUP" ControlToValidate="
                    txtUSPhoneNumber" ForeColor="Red"
                    ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}">
                    </asp:RegularExpressionValidator>


1 comment:

  1. The RegularExpressionValidator control is used to ensure that an input value matches a specified pattern. . This Validator control is ASP.NET control.
    very thanks to who post the blog am really find what i expected .
    Thanks for your valid and detailed information
    dot net course fees | dot net training in velachery

    ReplyDelete