
The difference between javascript and asp.net regular expression for email, number validation
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.
-
Related Reading
- Javascript multidimensional array (create, add and r
- Remove text after clicking in the textbox and then d
- Javascript get referrer url(previous page url), curr
- Javascript get current url, domain from url, relativ
- Javascript convert hex to decimal, binary to hex, oc
- Javascript delay loading images to improve web page
- Remove html element javascript by their parent or cl
- Javascript get all input elements in form and all im
- Javascript sets the element height that cannot be au
- Javascript create element(div,li,img,span) dynamical
- Javascript click child element to add onclick to par
- Mod operator and rounding off numbers in javascript