Asp.net Regular Expression Validator control



The RegularExpressionValidator verification control is used to ensure that any text box will accept the predefined format of the characters. This format can be of any type like you@domain.com (a valid email address).

Following are main properties of the validation control.
ValidationExpressionGets or sets the regular expression that will be used to validate input control data.
ControlToValidateGets or sets the input control to validate (eg. The ID value of asp:TextBox control).
DisplayDynamic/Static. Used to indicate how the area of error message will be allocated.
Dynamic: Error message area will only be allocated when error will be displayed. Static: Error messagea area will be allocated in either case.
Enabledtrue/false. Gets or sets whether to enable the validation control or not.
ErrorMessageGets or sets the text of the error message that will be displayed when validation fails (This is displayed when ValidationSummary validatoin control is used.).
TextGets or sets the description of the error message text.
ValidationGroupGets or sets the validation group it belongs to. This is used to group a set of controls.
SetFocusOnErrortrue/false. Used to move focus on the control that fails the validation.

Example - 

 <asp:Label ID="lbl" AssociatedControlID="TextBox1" runat="Server" Text="Write into TextBox"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="Server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="req1" runat="Server" ControlToValidate="TextBox1" ErrorMessage="TextBox is Mandatory field" Text="Please write something in the Box."></asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" Text="Write your email" ErrorMessage="eg. you@domain.com"
            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
        <asp:Button ID="btnSubmit" runat="Server" OnClick="WriteTextBoxValue" Text="Submit" />               
        <asp:ValidationSummary ID="ValidationSummary" runat="Server" ShowMessageBox="true" />
                    
Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment