JS verification of the mailbox, telephone, digital, postcode HTML code:
<TITLE>JS Verification of mobile email </title>
<body>
The code is as follows |
Copy Code |
<script language= "javascript" type= "Text/javascript" > string.prototype.isnull=testnull;//to determine if it is empty string.prototype.number=testnumber;//to judge numbers, only integers string.prototype.doublenumber=testdoublenumber;//to judge the number, can be decimal string.prototype.zip=testzip;//to judge the postal code format string.prototype.phone=testphonenumber;//to determine the telephone number, fax format string.prototype.email=testemail;//to judge the mailbox format To determine if NULL, NULL to put back true function Testnull () { if (This.replace (^s*) | ( s*$)/g), ""). Length<=0) { Reurn true; is empty } Else { return false; is not empty } } |
1. Determines whether a number is a number and returns True
The code is as follows |
Copy Code |
function Testnumber () { if (!this.isnull) { for (i=0;i<this.length;i++) { if (This.charat (i) < "0" | | this.charat (i) > "9") { return false; } } return true; } Else { return true; } } |
2. To determine the postal code format, return true correctly
The code is as follows |
Copy Code |
function Textzip () { if (!this.isnull) { if (this.length!=6) { return False } Else { var restel=/^[0-9]+$/; if (!restel.test (this)) { return false; } } } return true; } |
To determine the phone number, fax format, correctly put back true
The code is as follows |
Copy Code |
function Testphonenumber () { if (!this.isnull ()) { var reg=/(^[0-9]{3,4}-[0-9]{7,8}-[0-9]{3-4$}) | (^[0-9]{3,4}-[0-9]{7-8}$) | (^[0-9]{7-8}-[0-9]{3,4}$) | (^[0-9]{7,15}$)/; if (!reg.test (this)) { return false; } return true; } Else { return true; } } |
Determine e-mail format and return true in good form
The code is as follows |
Copy Code |
function Testemail () { if (!this.isnull ()) { if (This.search (/^) ([-_a-za-z0-9.] +) @ ([_a-za-z0-9]+.) +[a-za-z0-9]{2,3}$/)!=-1) { return true; } return false; } Else { return true; } } |
Determines whether a number, can be a decimal, and returns true in the correct format
The code is as follows |
Copy Code |
function Testdoublenumber () { var pointcount=0; Define a variable decimal point with an initial value of 0 for (Var i=0;i<this.length;i++) { if (This.charat (i) < "0" | | This.charat (i) > "9") &&this.charat (i)!= "." { return false; } Else { if (This.charat (i) = = '. ') pointcount++; } } if (pointcount>1) { return false; } else if (Pointcount==1&&this.trim (). Length==1)//Judge the decimal point is only 1 digits, and the length is 1, there is only a small points, of course, return false { return false; } return true; } </body> |