Tuesday, 14 April 2015

What Regular expression validator is and how to validate the Phone or Mobile numbers ASP.NET

In this article I will explain what Regular expression validator is and how to validate the Phone or Mobile numbers ASP.NET

Regular expression validator is the most common validation expressions used to validate an Email ID and phone number.

It is necessary to specify that RegularExpressionValidator is validating which control. In theControlToValidate property, we specify the control which will be validated. For this, we need to mention the ID of the textbox control.

India Mobile validation expression: ValidationExpression="^[\s\S]{10,12}$"
US Mobile validation expression: ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"

Example:

 
  <asp:TextBox ID="txtMobile" runat="server" CssClass="txtbox" Width="85%" MaxLength="12"></asp:TextBox>
                                    <asp:RegularExpressionValidator Display="Dynamic" ControlToValidate="txtMobile" ID="RegularExpressionValidator3"
                                        ValidationExpression="^[\s\S]{10,12}$" runat="server" ValidationGroup="vgSave" ForeColor="Red"
                                        ErrorMessage="Minimum 10 and Maximum 12 characters required.">*</asp:RegularExpressionValidator>
                                    <ajax:FilteredTextBoxExtender ID="FilteredTextBoxExtender4" runat="server" TargetControlID="txtMobile"
                                        FilterType="Custom, Numbers" Enabled="True" />
                                    <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator6" ControlToValidate="txtMobile"
                                        Display="Dynamic" ErrorMessage="Mobile is required" ForeColor="Red" ValidationGroup="vgSave">*

                                    </asp:RequiredFieldValidator>

No comments:

Post a Comment