JS How to verify the mailbox, telephone, digital, postcode

Source: Internet
Author: User

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>

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.