Common regular expressions are as follows:
Copy Code code as follows:
"^-? [1-9]\\d*$],//integer
"^[1-9]\\d*$",//Positive integer
"^-[1-9]\\d*$",//Negative integer
"^([+-]?) \\d*\\.? \\d+$ ",//number
"^[1-9]\\d*|0$",//positive number (positive integer + 0)
"^-[1-9]\\d*|0$",//negative (negative integer + 0)
"^([+-]?) \\d*\\.\\d+$ ",//floating-point numbers
"^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$",//Positive floating-point numbers
"^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*) $",//negative floating-point number
"^-? ([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?. 0+|0) $ ",//floating-point number
"^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?" he said. 0+|0$ ",//nonnegative floating-point number (positive floating-point number + 0)
"^ (-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)) |?" 0+|0$ ",//non-positive floating-point number (negative floating-point number + 0)
"^\\w+ (-\\w+) | ( \\.\\w+)) *\\@[a-za-z0-9]+ (\\.| -) [a-za-z0-9]+) *\\. [a-za-z0-9]+$],//Mail
"^[a-fa-f0-9]{6}$",//Color
"^http[s]?:\ \/\\/([\\w-]+\\.) +[\\w-]+ ([\\w-./?%&=]*)? $ ",//url
"^[\\ a-\\ 龥 \\?-\\?] +$ ",//Only Chinese
"^[\\x00-\\xff]+$",//only acsii characters
"^\\d{6}$",//ZIP code
"^ (13|15|18) [0-9]{9}$",//Mobile phone
"^ (25[0-5]|2[0-4]\\d| [0-1]\\d{2}| [1-9]?\\d] \. (25[0-5]|2[0-4]\\d| [0-1]\\d{2}| [1-9]?\\d] \. (25[0-5]|2[0-4]\\d| [0-1]\\d{2}| [1-9]?\\d] \. (25[0-5]|2[0-4]\\d| [0-1]\\d{2}| [1-9]?\\d) $ ",//IP address
"^\\s+$",//Non-empty
"(.*)\\. (JPG|BMP|GIF|ICO|PCX|JPEG|TIF|PNG|RAW|TGA) $ ",//Picture
"(.*)\\. (rar|zip|7zip|tgz) $ ",//Compressed file
"^\\d{4}" (\\-|\\/|\.) \\d{1,2}\\1\\d{1,2}$ ",//Date
"^[1-9]*[1-9][0-9]*$",//qq number
"^ ([0\\+]\\d{2,3}-)?" (0\\d{2,3})? (\\d{7,8}) (-(\\d{3,}))? $ ",//Phone number function (including verification of domestic area code, international area code, extension number)
"^\\w+$",//for user registration. Match a string of numbers, 26 English letters, or underscores
"^[a-za-z]+$",//Letter
"^[a-z]+$",//capital letters
"^[a-z]+$",//lowercase letters
"^[1-9] ([0-9]{14}|[ 0-9]{17}) $ "//ID Card
The following is a simple example:
Copy Code code as follows:
function Chknum (obj) {
var val = obj.val;
if (val!= "") {
if (!) ( /^[0-9]{1,20}$/.exec (val))) {
Obj.val= "";
Alert ("Please enter a valid number!");
Obj.focus ();
}
}
}
Two additional sites recommended by the cloud communities:
JavaScript Form Validation Regular expression encyclopedia [recommended]
Example code for JAVASCRIPT using regular expressions for form validation