[Practical] common JS verification functions

Source: Internet
Author: User

[Code language = js]/******************************* *************************************
* Common JS verification functions
* QQ: 12165743
* Email: wqb7156@163.com
* Date: 2007-06-06
**************************************** ******************************

Usage instructions: true is returned for correct matching; otherwise, false is returned.
----------------------------------------------------------------------
Function list:
CheckInput () Control Verification
IsLen () Judge Length
IsUsername () authentication Username
IsPassword () Verification Password
IsEn () verify whether it is a letter
IsCn () verifies whether it is a Chinese character
IsEmail () verification email
IsMSN () Verification MSN
IsQQ () Verification QQ number
IsHttpUrl () Verification URL
IsDoMainName () verification domain name
IsIpv4 () authentication IPV4 address
IsImgURL () verifies the image address
IsCellPhone () Verification mobile phone number
IsPhone () Verification
IsZipCode () Verification zip code
IsIdCard () Verification ID card number
IsDateFormat () Validate Date Format YY-MM-DD
IsRangeTime () Verification time format HH: MM: SS
IsMoney () verification amount format
IsInt () verifies the number and determines its range
IsSearch () Validation keyword
-----------------------------------------------------------------------
Example:
<Script language = "javascript">
Var email = "username ";
Alert (email. IsEmail ());
</Script>

<Input name = "email" type = "text" id = "email" value = "wqb7156 # 163.com"/>
<Div id = "Megdiv"> </div>
<Script language = "javascript">
If (! CheckInput ("email", "notnull", 'ismail', "Megdiv") return flase;
</Script>
----------------------------------------------------------------------*/
// Control Verification CheckInput (Id, Name, Title, Null, Format, LengthMin, LengthMax, Megdiv)
Function CheckInput ()
{
Var Id = arguments [0];
Var Name = arguments [1];
Var Title = arguments [2];
Var Null = arguments [3]. toLowerCase ();
Var Format = arguments [4]
Var LengthMin = arguments [5]
Var LengthMax = arguments [6];
Var Megdiv = (arguments [7] = null )? Id + '_ meg': arguments [7];

Var obj = document. getElementById (Id );
Var Name = document. getElementsByName (Name );
Var value = obj. value. replace (/(^ \ s *) | (\ s * $)/g ,"");
Var type = obj. type. toLowerCase ();
Var showdiv = document. getElementById (Megdiv );


// Whether it is null. If not, the length range is determined.
If (Null = 'notnull ')
{
Switch (type ){
// File domain
Case "file ":
If (value = '')
{
Showdiv. innerHTML = '× select the image you want to upload ';
Obj. focus ();
Return false;
}
// Text box
Case "text ":
If (value = '')
{
Showdiv. innerHTML = '×' + Title + 'cannot be blank ';
Obj. focus ();
Return false;
}
Break;
// Text field
Case "textarea ":
If (value = '')
{
Showdiv. innerHTML = '×' + Title + 'cannot be blank ';
Obj. focus ();
Return false;
}
Break;
// Password box
Case "password ":
If (value = '')
{
Showdiv. innerHTML = '×' + Title + 'cannot be blank ';
Obj. focus ();
Return false;
}
// Confirm the password
If (Id. substr (Id. length-2, 2) = 'Re ')
{
Var pwd = Id. substr (0, Id. length-3 );
If (document. getElementById (pwd). value! = Value)
{
Showdiv. innerHTML = '×' + 'different passwords entered twice ';
Obj. focus ();
Return false;
}
}
Break;
// Single-choice button
Case "radio ":
Var flag = false;
For (var I = 0; I <Name. length; I ++)
{
If (Name. checked) flag = true;
// Break;
}
If (! Flag)
{
Showdiv. innerHTML = '×' + Title + 'cannot be blank ';
Return false;
}
Break;
// Check box
Case "checkbox ":
Var flag = false;
For (var I = 0; I <Name. length; I ++)
{
If (Name. checked) flag = true;
// Break;
}
If (! Flag)
{
Showdiv. innerHTML = '×' + Title + 'cannot be blank ';
Return false;
}
Break;
// List box
Case "select-one ":
If (obj. selectedIndex = 0)
{
Showdiv. innerHTML = '×' + Title + 'cannot be blank ';
Obj. focus ();
Return false;
}
Break;
}
} Else
{
// Confirm the password
If (type = 'Password' & Id. substr (Id. length-2, 2) = 'Re ')
{
Var pwd = Id. substr (0, Id. length-3 );
If (document. getElementById (pwd). value! = Value)
{
Showdiv. innerHTML = '×' + 'different passwords entered twice ';
Obj. focus ();
Return false;
}
}
}
// Determine the length
If (value! = ''&&! Value. IsLen (LengthMin, LengthMax ))
{
Showdiv. innerHTML = '×' + Title +' the length must be between '+ LengthMin +' and '+ LengthMax + ';
Obj. focus ();
Return false;
}
// Verify the data format
If (Format! = 'Nocheck' & value! = '')
{
// Select a function
Switch (Format)
{
Case "IsLen": Format = value. IsLen (); break;
Case "IsUsername": Format = value. IsUsername (); break;
Case "IsPassword": Format = value. IsPassword (); break;
Case "IsEn": Format = value. IsEn (); break;
Case "IsCn": Format = value. IsCn (); break;
Case "IsEmail": Format = value. IsEmail (); break;
Case "IsMSN": Format = value. IsMSN (); break;
Case "IsQQ": Format = value. IsQQ (); break;
Case "IsHttpUrl": Format = value. IsHttpUrl (); break;
Case "IsDoMainName": Format = value. IsDoMainName (); break;
Case "IsIpv4": Format = value. IsIpv4 (); break;
Case "IsImgURL": Format = value. IsImgURL (); break;
Case "IsCellPhone": Format = value. IsCellPhone (); break;
Case "IsPhone": Format = value. IsPhone (); break;
Case "IsZipCode": Format = value. IsZipCode (); break;
Case "IsIdCard": Format = value. IsIdCard (); break;
Case "IsDateFormat": Format = value. IsDateFormat (); break;
Case "IsRangeTime": Format = value. IsRangeTime (); break;
Case "IsMoney": Format = value. IsMoney (); break;
Case "IsInt": Format = value. IsInt (); break;
Case "IsSearch": Format = value. IsSearch (); break;
}
If (! Format)
{
Showdiv. innerHTML = '×' + Title +' incorrectly formatted ';
Obj. focus ();
Return false;
}
}
Showdiv. innerHTML = "√ ";
Return true;
}

// Determine the length (the length is not limited to 0)
String. prototype. IsLen = function ()
{
Var isRightFormat = false;
Var minnum = arguments [0]? Arguments [0]: 0;
Var maxnum = arguments [1]? Arguments [1]: 0;
IsRightFormat = (minnum = 0 & maxnum = 0? True: (calculate_byte (this)> = minnum & calculate_byte (this) <= maxnum? True: false ));
Return isRightFormat;
}

// Verify the user name
String. prototype. IsUsername = function ()
{
Var myReg =/^ [0-9a-zA-Z \-] + $ /;
If (myReg. test (this) return true;
Return false;
}
// Verify the password
String. prototype. IsPassword = function ()
{
Var myReg =/^ [0-9a-zA-Z] + $ /;
If (myReg. test (this) return true;
Return false;
}
// Verify whether it is a letter
String. prototype. IsEn = function ()
{
Var myReg =/^ [a-zA-Z] + $ /;
If (myReg. test (this) return true;
Return false;
}

// Verify whether it is a Chinese character
String. prototype. IsCn = function ()
{
Var myReg =/^ [\ u0391-\ uFFE5] + $ /;
If (myReg. test (this) return true;
Return false;
}

// Verify email
String. prototype. IsEmail = function ()
{
Var myReg =/([\ w-\.] +) @ (\ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\.) | ([\ w-] + \.) +) ([a-zA-Z] {2, 4} | [0-9] {1, 3}) (\]?) /;
If (myReg. test (this) return true;
Return false;
}

// Verify MSN
String. prototype. IsMSN = function ()
{
Var myReg =/([\ w-\.] +) @ (\ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\.) | ([\ w-] + \.) +) ([a-zA-Z] {2, 4} | [0-9] {1, 3}) (\]?) /;
If (myReg. test (this) return true;
Return false;
}

// Verify the QQ number
String. prototype. IsQQ = function ()
{
Var myReg =/^ [1-9] \ d {4, 9} $ /;
If (myReg. test (this) return true;
Return false;
}
// Verify the URL
String. prototype. IsHttpUrl = function ()
{
Var myReg =/^ http: \/[A-Za-z0-9] + \. [A-Za-z0-9] + [\/= \? % \-&_~ '@ [\] \': +!] * ([^ <> \ "\"]) * $ /;
If (myReg. test (this) return true;
Return false;
}

// Verify the Domain Name
String. prototype. IsDoMainName = function ()
{
Var myReg =/^ [0-9a-zA-Z] ([0-9a-zA-Z \-] + \.) {1, 3} [a-zA-Z] {2, 4 }? $ /;
If (myReg. test (this) return true;
Return false;
}

// Verify the IPV4 address
String. prototype. IsIpv4 = function ()
{
Var myReg =/^ (2 [0-5] {2} | 1? [0-9] {1, 2}). (2 [0-5] {2} | 1? [0-9] {1, 2}). (2 [0-5] {2} | 1? [0-9] {1, 2}). (2 [0-5] {2} | 1? [0-9] {1, 2}) $ /;
If (myReg. test (this) return true;
Return false;
}

// Verify the image address (images dynamically generated by CGI are not supported)
String. prototype. IsImgURL = function ()
{
Var myReg =/^ \. (jpeg | jpg | gif | bmp | png | pcx | tiff | tga | lwf) $ /;
If (myReg. test (this) return true;
Return false;
}

// Verify the mobile phone number
String. prototype. IsCellPhone = function ()
{
Var myReg =/^ (\ d {3} \) | (\ d {3 }\-))? 1 [3, 5] \ d {9} $ /;
If (myReg. test (this) return true;
Return false;
}

// Verify the landline
String. prototype. IsPhone = function ()
{
Var myReg =/^ [+] {0, 1} (\ d) {1, 3} []? ([-]? (\ D) | []) {1, 12}) + $ /;
If (myReg. test (this) return true;
Return false;
}

// Verify the zip code
String. prototype. IsZipCode = function ()
{
Var myReg =/[0-9] {6 }/;
If (myReg. test (this) return true;
Return false;
}

// Verify the ID card number
String. prototype. IsIdCard = function ()
{
Var myReg =/(^ ([\ d] {15} | [\ d] {18} | [\ d] {17} [xX] {1}) $) /;
If (myReg. test (this) return true;
Return false;
}

// Validate Date Format YY-MM-DD
String. prototype. IsDateFormat = function ()
{
Var myReg =/^ [0-9] {4}-[0-9] {1, 2}-[0-9] {1, 2} $ /;
If (myReg. test (this) return true;
Return false;
}

// Verification time format HH: MM: SS
String. prototype. IsRangeTime = function ()
{
Var myReg =/^ (\ d {2}) :( \ d {2}) :( \ d {2}) $ /;
If (myReg. test (this) return true;
Return false;
}

// Verify the amount format
String. prototype. IsMoney = function ()
{
Var myReg =/^ [0-9] {} [.] {} [0-9] {} $ /;
If (myReg. test (this) return true;
Return false;
}

// Verify the digit format and determine the circumference of the digit (min: min; max: maximum .)
String. prototype. IsInt = function ()
{
Var isRightFormat = false;
Var minnum = arguments [0]? Arguments [0]: 0;
Var maxnum = arguments [1]? Arguments [1]: 0;
Var myReg =/^ [-\ +]? \ D + $ /;
If (myReg. test (this ))
{
IsRightFormat = (minnum = 0 & maxnum = 0? True: (this> minnum & this <maxnum? True: false ));
}
Return isRightFormat;
}

// Verify the search keyword
String. prototype. IsSearch = function ()
{
Var myReg =/^ [\ | \ "\ '<>,. * & @ # $ ;:! ^ ()]/;
If (myReg. test (this) return false;
Return true;
}

// Js accurately calculates the string length
Function calculate_byte (sTargetStr ){
Var sTmpStr, sTmpChar;
Var nOriginLen = 0;
Var nStrLength = 0;

STmpStr = new String (sTargetStr );
NOriginLen = sTmpStr. length;

For (var I = 0; I <nOriginLen; I ++ ){
STmpChar = sTmpStr. charAt (I );

If (escape (sTmpChar). length> 4 ){
NStrLength + = 2;
} Else if (sTmpChar! = '\ R '){
NStrLength ++;
}
}

Return nStrLength;
}


From jiutian galaxy

Related Article

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.