Saturday 22 March 2014

regex validator in asp.net

Regex ValidationCheck that the value in the validated control has specific defined range. user's input is in a given range(Number or Character).  
Event will fire or control will validate only once user click on Button .
Important Property which need to handle for Regular Expression Field validation is;
ControlToValidate – Linked with id which control to validate;

EnableClientScript - specifies client-side validation is enabled or not (true/false)

Display – Contain three value None- the control is not displayed; Static - Control displays an error message if it’s not validate. Space is kept on the page for the message; Dynamic - Control displays an error message if it’s not validate. Space is not kept on the page for the message
;
Enabled - specifies whether the validation control is enabled or not(true/false)


ErrorMessage - text to display in the ValidationSummary control when validation fails, it display in validation control also

ForeColor – Text color of message

Text - message to display when validation fails

ValidationGroup – Grouping all the validation control together

@Code Snippet – Html code     Validation of phone number

   <form id="form1" runat="server">
    <div>
        <table width="60%" align="center">
            <tr>
                <td colspan="2" align="left">
                    <h1>
                        Check validation</h1>
                </td>
            </tr>
            <tr>
                <td align="right">
                    Name :
                </td>
                <td align="left">
                    <asp:TextBox ID="txtValue" runat="server"></asp:TextBox>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator2" ControlToValidate="txtValue"
                        ValidationExpression="^([A-Za-z]{0,15})$" EnableClientScript="false" ForeColor="Red" ErrorMessage="Invalid Name!!"
                        runat="server" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                     
                </td>
            </tr>
            <tr>
                <td align="right">
                    Phone Number :
                </td>
                <td align="left">
                    <asp:TextBox ID="txtPhoneNumber" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" Display="Dynamic" runat="server"
                        ControlToValidate="txtPhoneNumber" ForeColor="Red" ErrorMessage="Enter Phone Number !!"></asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtPhoneNumber"
                        ValidationExpression="^(?:(?:01\d{9}|2\d{7}) ){1,}(?:01\d{9}|2\d{7})$" EnableClientScript="false" ForeColor="Red" ErrorMessage="Invalid Phone Number!!"
                        runat="server" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                     
                </td>
            </tr>
           
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="Button1" runat="server" Text="Button" />
                </td>
            </tr>
        </table>
    </div>
    </form>

 In above code validating name and Validating phone number regex

@OutPut screen
Once user not enter any value in text box and click on Button;

No comments:

Post a Comment