A friend asked me how to verify my js email address, phone number, number, and zip code. Below I will collect a few functions about js verification email address, phone number, number, and zip code to share with you.
Javascript verification email, phone number, number, zip code HTML code:
<Html>
<Head>
<Title> mobile phone number verified by js </title>
</Head>
<Body>
| The Code is as follows: |
Copy code |
<Script language = "javascript" type = "text/javascript">
String. prototype. isNull = testNull; // determines whether it is null.
String. prototype. number = testNumber; // determines a number. It can only be an integer.
String. prototype. doubleNumber = testDoubleNumber; // determines a number. It can be a decimal number.
String.prototype.zip = testZip; // determine the zip code format
String. prototype. phone = testPhoneNumber; // determines the contact number and fax format.
String. prototype. email = testEmail; // determines the email format.
// Judge whether it is null. If it is null, return true.
Function testNull ()
{
If (this. replace (/(^ s *) | (s * $)/g), ""). length <= 0)
{
Reurn true; // null
}
Else
{
Return false; // not empty
}
} |
1. // determines whether it is a number. If it is a number, true is returned.
| 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. // determine the zip code format. If the format is correct, true is returned.
| 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;
} |
// Determine the contact number and fax format. The format is correct and set to 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 the email format. If the format is correct, true is returned.
| 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 it is a number. It can be a decimal number. If the format is correct, true is returned.
| The Code is as follows: |
Copy code |
Function testDoubleNumber ()
{
Var pointCount = 0; // defines the decimal point of the variable. The initial value is 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) // determines that the decimal point is only one digit and the length is also 1. Only one decimal point exists. Of course, false is returned.
{
Return false;
}
Return true;
}
</Body>
</Html> |