28 JS Validation Function Collection _ form effects

Source: Internet
Author: User
Tags getdate
JS validation function of the call method and note:
1. Set the form onsubmit= "return test ()", with <input type= "Submit" name= "Submit" value= "Check" > Submit.
2.<input type=text onkeydown= "test ();" >//When the mouse is pressed, call function validation directly
3.<input type=text onblur= "Testl (this.value)" >//Pass in its own value for verification, test (this)//incoming object itself for verification
4.<input type= "button" value= "Test" onclick= "Test ()" >//Click BTN to verify, if passed then use Document.forms[0].submit (); Make a form submission
5.<input onclick= "return test ();" type= "Submit" name= "Submit" value= "Submission" >//use submit for submission verification, if Test return false; Returns true commits are not committed.
6.<script type= "javascript" >....function Test () {... return false; ...</script>
7.| | With,&& or,! non
8. Frequently used events: Onblur loses focus, onchange loses focus and content changes, onfocus element gets focus, onreset triggers this event when the Reset property in the form is fired, when the OnSubmit form is submitted
9. Verify Regular expression: if (/^[1-9]\d*$/.test (str)) return true pass, False pass
10.document.getelementbyid ("IP"). value//Access ID value, document.form1.text1.value//access via name
11.<input onblur= "if (This.value.replace (/^ +| +$/g,") = = ") alert (' cannot be empty! ')" >//Execution of a single regular validation case
Validation function:
<input type= "text" name= "Text1 id=" Text1 "onblur=" Test (); "Value=" "/>"
1. Character length limit
function test ()
{
if (DOCUMENT.FORM1.TEXT1.VALUE.LENGTH&GT;50)
{
Alert ("Cannot exceed 50 characters!") ");
Document.form1.text1.focus ();
return false;
}
}
2. Can only be in English, letters or numbers
function test ()
{
if (!) ( EVENT.KEYCODE&GT;=65&AMP;&AMP;EVENT.KEYCODE&LT;=90))
{
Alert ("Can only be English!") ");
Document.form1.text1.focus ();
}
}
<input onblur= "if (/[^0-9a-za-z]/g.test (this.value)) alert (' Error ') ' >//Letter or number
3. Only the numbers
function test ()
{
if (!) ( (event.keycode>=48&&event.keycode<=57) | | (event.keycode>=96&&event.keycode<=105))) Consider the number keys on the keypad
{
Alert ("Only a number!") ");
Document.form1.text1.focus ();
}
}
Or
function test (NUM)
{
var i,j,strtemp;
strtemp= "0123456789";
if (num.length== 0)
return 0
for (i=0;i<num.length;i++)
{
J=strtemp.indexof (Num.charat (i));
if (j==-1)
{
Alert ("Only a number!") ");
return false;
}
}
Description is a number
return true;
}
5. Verify the mailbox (regular, function)
function Isemail (stremail) {
if (Stremail.search (-\w+) | (/^\w+) | ( \.\w+)) *\@[a-za-z0-9]+ (\.| -) [a-za-z0-9]+) *\. [a-za-z0-9]+$/)!=-1)
return true;
Else
Alert ("Malformed!");
}
function Isemail () {
if (document.userinfo.useremail.value.charAt (0) = = "." | |
Document.userinfo.useremail.value.charAt (0) = = "@" | |
Document.userinfo.useremail.value.indexOf (' @ ', 0) = = 1 | |
Document.userinfo.useremail.value.indexOf ('. ', 0) = = 1 | |
Document.userinfo.useremail.value.lastIndexOf ("@") ==document.userinfo.useremail.value.length-1 | |
Document.userinfo.useremail.value.lastIndexOf (".") ==DOCUMENT.USERINFO.USEREMAIL.VALUE.LENGTH-1)
{
Alert ("Email address format is not correct!") ");
Document.userinfo.useremail.focus ();
return false;
}
}
<input type= "text" onblur= "Isemail (this.value);" Name= "Text1"/>
6. Shielding keywords (here shield * * * * and * * * *)
function Test () {
if ((Document.form1.text1.value.indexOf ("* *") = = 0) | | (Document.form1.text1.value.indexOf ("* * *") = = 0)) {
Alert ("Presence of keywords");
Document.form1.text1.focus ();
return false;
}
}
7. Compare two times input is the same
if (Document.userinfo.userpassword.value!= document.userinfo.userpassword1.value) {
Document.userinfo.userpassword.focus ();
Document.userinfo.userpassword.value = ';
Document.userinfo.userpassword1.value = ';
Alert ("Two input passwords are different, please re-enter!") ");
return false;
}
8. Determine whether the composition is empty or blank
function Test () {
if (Checkspace (Document.form1.text1.value)) {
Document.form1.text1.focus ();
Alert ("is empty or consists of spaces!") ");
return false;
}
}
function Checkspace (CHECKSTR) {
var str = ';
for (i = 0; i < checkstr.length; i++) {
str = str + ';
}
return (str = = CHECKSTR);
}
Or:
<input onblur= "if (This.value.replace (/^ +| +$/g,") = = ") alert (' cannot be empty! ')" >//Execution of a single regular validation case
9. Verify that it is a digital phone, only numbers and-
Regular: \d{3}-\d{8}|\d{4}-\d{7}
function Istel (elem) {
var Str=elem.value;
var Onedecimal=false;
var onechar=0;
Str=str.tostring ();
for (var i=0; i<str.length; i++) {
Onechar=str.charat (i). charCodeAt (0);
if (onechar==45) {continue;}
if (onechar<48 | | Onechar > 57) {
Alert ("This entry can only enter numbers and '-' numbers.");
return false;
}
}
return true;
}
Or
function Fucchecktel (TEL)
{
var i,j,strtemp;
Strtemp= "0123456789-() #";
for (i=0;i<tel.length;i++)
{
J=strtemp.indexof (Tel.charat (i));
if (j==-1)
{
Alert ("This entry can only enter numbers and '-' numbers.");
return false;
}
}
Description Legal
return true;
}
Or
Check ordinary telephone, fax number: You can "+" start, in addition to numbers, can contain "-"
function Istel (s)
{
var patrn=/^[+]{0,1} (\d) {1,3}[]? ([-]? (\d) {1,12}) +$/;
var patrn=/^[+]{0,1} (\d) {1,3}[]? ([-]? ((\d) | []) {1,12}) +$/;
if (!patrn.exec (s)) return false
return True
}
Or
Requirements:
(1) The telephone number shall consist of numbers, "(") "and"-"
(2) Phone number is 3 to 8 digits
(3) If the phone number contains an area code, then the area code is three digits or four digits
(4) The area code is separated by "(", ")" or "-" and other parts
(5) Mobile phone number is 11 or 12 digits, if 12 digits, then the first digit is 0
(6) The first and second digits of the 11-digit mobile phone number are "13"
(7) The second and third digits of the 12-digit mobile phone number are "13"
According to these rules, you can go with the following regular expressions:
(^[0-9]{3,4}\-[0-9]{3,8}$) | (^[0-9]{3,8}$) | (^\ ([0-9]{3,4}\) [0-9]{3,8}$) | (^0{0,1}13[0-9]{9}$)
function Phonecheck (s) {
var str=s;
var reg=/(^[0-9]{3,4}\-[0-9]{3,8}$) | (^[0-9]{3,8}$) | (^\ ([0-9]{3,4}\) [0-9]{3,8}$) | (^0{0,1}13[0-9]{9}$)/
Alert (Reg.test (str));
}
<input type= "text" name= "Text1" onblur= "Istel (this);"/>
10.//when OPT2 is 1 o'clock check num is negative//when OPT1 is 1 o'clock to check if num is decimal//return 1 is correct, 0 is wrong
function Chknbr (NUM,OPT1,OPT2)
{
var i=num.length;
var Staus;
The number of staus used for recording.
status=0;
if ((opt2!=1) && (num.charat (0) = = '-")
{
Alert ("You have enter a invalid number.");
return 0;
}
An error occurred when the last one was.
if (Num.charat (i-1) = = '. ')
{
Alert ("You have enter a invalid number.");
return 0;
}
for (j=0;j<i;j++)
{
if (Num.charat (j) = = '. ')
{
status++;
}
if (status>1)
{
Alert ("You have enter a invalid number.");
return 0;
}
if (Num.charat (j) < ' 0 ' | | num.charat (j) > ' 9 ')
{
if ((opt1==0) | | (Num.charat (j)!= '. ')) && (j!=0))
{
Alert ("You have enter a invalid number.");
return 0;
}
}
}
return 1;
}
11. Check whether a string consisting of numbers or letters
function Test (str)
{
var strsource = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var ch;
var i;
var temp;
For (i=0;i<= (str.length-1); i++)
{
ch = str.charat (i);
temp = strsource.indexof (CH);
if (temp==-1)
{
Alert ("Improperly formatted!");
return false;
}
}
if (Strsource.indexof (CH) ==-1)
{
Alert ("Improperly formatted!");
return false;
}
Else
{
return true;
}
}
12. Digital Verification
Positive integer validation ^[1-9]\d*$, negative integer validation ^-[1-9]\d*$, Integer ^-? [1-9]\d*$, nonnegative integer ^[1-9]\d*|0$, positive positive ^-[1-9]\d*|0$, floating-point number ^-? ([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0) $
function Test (str) {
if (/^[1-9]\d*$/.test (str))
{
Alert ("Format cannot!");
return false;
}
Else
{
return true;
}
}
13. Identity card
Regular: \d{15}|\d{18}
14.IP Address
Regular: \d+\.\d+\.\d+\.\d+
15. Postal Code
Regular: [1-9]\d{5} (?! \d)
function Ispostalcode (s)
{
var patrn=/^[a-za-z0-9]{3,12}$/;
var patrn=/^[a-za-z0-9]{3,12}$/;
if (!patrn.exec (s)) return false
return True
}
16.QQ number
Regular: [1-9][0-9]{4,}
17.HTML Mark
Regular: \d{3}-\d{8}|\d{4}-\d{7}
18. Color values that are valid
function IsColor (color) {
var Temp=color;
if (temp== "") return true;
if (temp.length!=7) return false;
Return (Temp.search (/\\#[a-fa-f0-9]{6}/)!=-1);
}
19. Links that are valid
function Isurl (URL) {
var stemp;
var b=true;
Stemp=url.substring (0,7);
Stemp=stemp.touppercase ();
if ((stemp!= "http://") | | (URL.LENGTH&LT;10)) {
B=false;
}
return b;
}
20. Whether the cell phone number is valid
function IsMobile (_STR) {
var tmp_str = trim (_STR);
var pattern =/13\\d{9}/;
Return Pattern.test (TMP_STR);
}
Or
Check mobile phone Number: Must start with a number, in addition to the number, can contain "-"
function IsMobile (s)
{
var patrn=/^[+]{0,1} (\d) {1,3}[]? ([-]? ((\d) | []) {1,12}) +$/;
if (!patrn.exec (s)) return false
return True
}
21.IP address and Identification number verification (regular)
function Checkip ()
{
Obj=document.getelementbyid ("IP"). Value
IP Address
var exp=/^ (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) $/;
Id
var exp=/\b ((?!) \d\d\d) \d+|1\d\d|2[0-4]\d|25[0-5]) (\b|\.)) {4}/
var exp=/^ (\d{15}|\d{17}[x0-9])/
var exp=/^ ([\d]{15}|[ \d]{18}| [\d] {17} [x| X]) $/
var reg = Obj.match (exp);
if (reg==null)
{
Alert ("IP address is illegal!) ");
}
Else
{
Alert ("IP address is legal!) ");
}
}
22. Verify Login Name: You can only enter 5-20 letters beginning with a letter, with a number, "_", "." The string
function Isregisterusername (s)
{
var patrn=/^[a-za-z]{1} ([a-za-z0-9]|[. _]) {4,19}$/;
if (!patrn.exec (s)) return false
return True
}
23. Verify the user's name: You can enter only 1-30 characters that start with a letter
function Istruename (s)
{
var patrn=/^[a-za-z]{1,30}$/;
if (!patrn.exec (s)) return false
return True
}
24. Verify Password: Only 6-20 letters, numbers and underscores can be entered
function ispasswd (s)
{
var patrn=/^ (\w) {6,20}$/;
if (!patrn.exec (s)) return false
return True
}
25. Date Time Class
Short time, shaped like (13:04:06)
function Istime (str)
{
var a = Str.match (/^ (\d{1,2}) (:)? \d{1,2}) \2 (\d{1,2}) $/);
if (a = = null) {alert (' input parameter is not a time format '); return false;}
if (a[1]>24 | | a[3]>60 | | a[4]>60)
{
Alert ("Time format is not");
return False
}
return true;
}
Short date, form (2003-12-05)
function Strdatetime (str)
{
var r = Str.match (/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) $/);
if (r==null) return false;
var d= new Date (r[1], r[3]-1, r[4]);
Return (D.getfullyear () ==r[1]&& (D.getmonth () +1) ==r[3]&&d.getdate () ==r[4]);
}
Long time, shaped like (2003-12-05 13:04:06)
function Strdatetime (str)
{
var reg =/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2}) $/;
var r = Str.match (reg);
if (r==null) return false;
var d= new Date (r[1], r[3]-1,r[4],r[5],r[6],r[7]);
Return (D.getfullyear () ==r[1]&& (D.getmonth () +1) ==r[3]&&d.getdate () ==r[4]&&d.gethours () = =r[5]&&d.getminutes () ==r[6]&&d.getseconds () ==r[7]);
}
26. Judging characters are all made up of a-Z or a-Z letter
<input onblur= "if (/[^a-za-z]/g.test (this.value)) alert (' Error ')" >
27. Judging characters consist of letters and numbers, underscores, and dots. And the beginning can only be underlined and letters.
<input onblur= "If" (/^ ([A-za-z_]{1}) ([\w]*) $/g.test (this.value)) alert (' Error ') >
28. The value of the form can not be empty, can not exceed IMAX characters, can not be less than imix characters, input for Chinese judgments
function IsNull (elem) {
var pattern=/^\s+|\s+$/;
if (Elem.replace (/^\s+|\s$)/g, "") = = "") {
return false;
}else{
return true;
}
}
Function Imax (elem) {
if (Elem.length>imax) {
return false;
}else{
return true;
}
}
function Imix (elem) {
if (Elem.lengthreturn false;
}else{
return true;
}
}
function Ischinese (elem) {
var pattern=/[^\x00-\xff]/g;
if (Pattern.test (Elem)) {
Contains Chinese
return false;
}else{
Does not contain Chinese
return true;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.