The difference between javascript and asp.net regular expression for email, number validation

Lionsure 2020-06-19 Original by the website

Asp.net 2.0 also provides functions such as verifying numbers, contact phone numbers, and emails using regular expressions, but it is not very good to verify in some cases. For example, verify that if the drop-down list box has a value selected, and verify the length of the text in English and DBCS. At this time, it is necessary to use javascript to complete. Whether you use asp.net or javascript, the regular expression is the same; however, the regular expression assigned to the variable can not be enclosed in double quotes in javascript, this is the only difference. Let's take a look at how to use asp.net and javascript to verify email and numbers respectively.

 

I. Regular expression for email validation

If the textbox for inputting email that needs to be verified is as follows:

<asp:TextBox ID="tbEmail" runat="server" MaxLength="100" />

 

1. Asp.net verification code

<asp:RegularExpressionValidator ID="revEmail" runat="server" ControlToValidate="tbEmail" Display="Dynamic" ErrorMessage="Email is wrong!" ValidationExpression="(?:\w+\.?)*\w+@(?:\w+\.)+\w+" />

 

2. Javascript verification code

function isEmail(text)
       {
              var exp = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
              return exp.test(text);
       }

The two verification methods, the regular expression is exactly the same, but in javascript, the regular expression is assigned to the variable "exp" with "/^ $/" enclosed.

 

 

II. Regular expression for number validation

If the textbox for inputting number that needs to be verified is as follows:

<asp:TextBox ID="tbNumber" runat="server" MaxLength="12" />

 

1. Regular expression in asp.net for number validation

<asp:RegularExpressionValidator ID="revNumber" runat="server" ControlToValidate="tbNumber" Display="Dynamic" ErrorMessage="The input is not a number!" ValidationExpression="[0-9]{1,3}" />

 

2. Regular expression in javascript for number validation

function isNumber(text)
       {
              var exp = /^[0-9]{1,3}$/;
              return exp.test(text);
       }

The two verification methods are the same as the verification email.