Friday, 11 May 2012

Regular Expression Validation Control

Step 1) Validate TextBox against   Indian Telephone number
              for ex:   for 10 digits mobile number

Step 2)  Create ASP.NET WEB APPLICATION
              --> ADD new web page  default.aspx
              -->  add text box control
              --> add button control
              --> add regular expression validation control to textbox.

Step 3)  In Default.aspx



    <form id="form1" runat="server">
    <div>
 
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" />
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
        ErrorMessage="Mobile numner not correct" ControlToValidate="TextBox1"
         ValidationExpression="\d{10}"></asp:RegularExpressionValidator>
 
    </div>
    </form>

In this added textbox with TexBox1 as ID
                    Button control
                    Regular Expression
                                         ValidateExpression "\d{10}"  it checks for 10 digit mobile number
                                         Regular Expression Bind to TextBox1 as shown above.


Step 4) Run the Default.aspx

Output :   If u give >10 or <10 numbers  It displays "Mobile number not correct
                Otehrwise No error Message.




Example 2)   If Telephone number is Landline with (Area Code)-Telephone number
                        for ex:  040-2278876

                   In this case Regular Expression is : "\d{2,3}-\d{7}"
                                Test case1 ) 40-2278876  -- 2 digit area code
                                Test case2 ) 040-2278876  -- 3 digit area code