Page to verify the function with JavaScript

Source: Internet
Author: User
/*************************************** **************************************** *******//******************************* *****************************

/*************************************** **************************************** *******/
/************************************ Numbers verify *************************************** **/
/*************************************** **************************************** *******/
/**
* Check whether all the characters entered are numbers.
* Input: str string
* Return value: true or flase; true indicates a number.
*/
Function checkNum (str ){
Return str. match (/\ D/) = null;
}

/**
* Check whether the input string of characters is decimal.
* Input: str string
* Return value: true or flase; true indicates decimal number.
*/
Function checkDecimal (str ){
If (str. match (/^ -? \ D + (\. \ d + )? $/G) = null ){
Return false;
}
Else {
Return true;
}
}
/**
* Check whether the input string is an integer.
* Input: str string
* Return value: true or flase; true indicates decimal number.
*/
Function checkInteger (str ){
If (str. match (/^ [-+]? \ D * $/) = null ){
Return false;
}
Else {
Return true;
}
}
/*************************************** **************************************** *******/
/************************************ Characters verify *************************************** **/
/*************************************** **************************************** *******/

/**
* Check whether a string of characters is a character.
* Input: str string
* Return value: true or flase. true indicates that all characters do not contain Chinese characters.
*/
Function checkStr (str ){
If (/[^ \ x00-\ xff]/g. test (str )){
Return false;
}
Else {
Return true;
}
}

/**
* Check whether the input string contains Chinese characters.
* Input: str string
* Return value: true or flase; true indicates that Chinese characters are included.
*/
Function checkChinese (str ){
If (escape (str). indexOf ("% u ")! =-1 ){
Return true;
}
Else {
Return false;
}
}

/**
* Check whether the entered email format is correct.
* Input: str string
* Return value: true or flase; true indicates that the format is correct.
*/
Function checkEmail (str ){
If (str. match (/[A-Za-z0-9 _-] + [@] (\ S *) (net | com | cn | org | cc | TV | [0-9 }) (\ S *)/g) = null ){
Return false;
}
Else {
Return true;
}
}

/**
* Check whether the entered mobile phone number is in the correct format.
* Input: str string
* Return value: true or flase; true indicates that the format is correct.
*/
Function checkMobilePhone (str ){
If (str. match (/^ (? : 13 \ d | 15 [89])-? \ D {5} (\ d {3} | \ * {3}) $/) = null ){
Return false;
}
Else {
Return true;
}
}

/**
* Check whether the entered landline number is correct
* Input: str string
* Return value: true or flase; true indicates that the format is correct.
*/
Function checkTelephone (str ){
If (str. match (/^ ([0 \ +] \ d {2, 3 }-)? (0 \ d {2, 3})-) (\ d {7, 8}) (-(\ d {3 ,}))? $/) = Null ){
Return false;
}
Else {
Return true;
}
}
/**
* Check whether the QQ format is correct.
* Input: str string
* Return value: true or flase; true indicates that the format is correct.
*/
Function checkQQ (str ){
If (str. match (/^ \ d {5, 10} $/) = null ){
Return false;
}
Else {
Return true;
}
}
/**
* Check whether the entered ID card number is correct
* Input: str string
* Return value: true or flase; true indicates that the format is correct.
*/
Function checkCard (str ){
// Regular Expression of the 15-digit ID card
Var arg1 =/^ [1-9] \ d {7} (0 \ d) | (1 [0-2]) ([0 | 1 | 2] \ d) | 3 [0-1]) \ d {3} $ /;
// Regular Expression of 18-digit ID card
Var arg2 =/^ [1-9] \ d {5} [1-9] \ d {3} (0 \ d) | (1 [0-2]) ([0 | 1 | 2] \ d) | 3 [0-1]) (\ d {4 }) | \ d {3} [A-Z]) $ /;
If (str. match (arg1) = null & str. match (arg2) = null ){
Return false;
}
Else {
Return true;
}
}
/**
* Check whether the Entered IP address is correct.
* Input: str string
* Return value: true or flase; true indicates that the format is correct.
*/
Function checkIP (str ){
Var arg =/^ (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) $ /;
If (str. match (arg) = null ){
Return false;
}
Else {
Return true;
}
}
/**
* Check whether the entered URL address is correct
* Input: str string
* Return value: true or flase; true indicates that the format is correct.
*/
Function checkURL (str ){
If (str. match (/(http [s]? | Ftp): \/[^ \/\.] +? \ .. + \ W $/I) = null ){
Return false
}
Else {
Return true;
}
}
/**
* Check whether the entered characters have special characters
* Input: str string
* Return value: true or flase; true indicates that the string contains special characters.
* It is mainly used for verification when registering information.
*/
Function checkQuote (str ){
Var items = new Array ("~ ","'","! "," @ "," # "," $ "," % "," ^ ","&","*","{","}", "[", "]", "(", ")");
Items. push (":", ";", "'", "|", "\", "<", "> ","? ","/"," <","> "," | ","//");
Items. push ("admin", "administrators", "administrator", "administrator", "System administrator ");
Items. push ("select", "delete", "update", "insert", "create", "drop", "alter", "trancate ");
Str = str. toLowerCase ();
For (var I = 0; I <items. length; I ++ ){
If (str. indexOf (items [I])> = 0 ){
Return true;
}
}
Return false;
}

/*************************************** **************************************** *******/
/************************************ Time verify *************************************** **/
/*************************************** **************************************** *******/
/**
* Check whether the date format is correct
* Input: str string
* Return value: true or flase; true indicates that the format is correct.
* Note: Chinese Date Format cannot be verified here
* Verification short date)
*/
Function checkDate (str ){
// Var value = str. match (/(^ (1 [8-9] \ d {2}) | ([2-9] \ d {3 }))(-) (10 | 12 | 0? [13578]) (-) (3 [01] | [12] [0-9] | 0? [1-9]) $) | (^ (1 [8-9] \ d {2}) | ([2-9] \ d {3 })) (-) (11 | 0? [2, 469]) (-) (30 | [12] [0-9] | 0? [1-9]) $) | (^ (1 [8-9] \ d {2}) | ([2-9] \ d {3 })) (-) (0? 2) (-) (2 [0-8] | 1 [0-9] | 0? [1-9]) $) | (^ ([2468] [048] 00) (-) (0? 2) (-) (29) $) | (^ ([3579] [26] 00) (-) (0? 2) (-) (29) $) | (^ ([1] [89] [0] [48]) (-) (0? 2) (-) (29) $) | (^ ([2-9] [0-9] [0] [48]) (-) (0? 2) (-) (29) $) | (^ ([1] [89] [2468] [048]) (-) (0? 2) (-) (29) $) | (^ ([2-9] [0-9] [2468] [048]) (-) (0? 2) (-) (29) $) | (^ ([1] [89] [13579] [26]) (-) (0? 2) (-) (29) $) | (^ ([2-9] [0-9] [13579] [26]) (-) (0? 2) (-) (29) $ ))/);
Var value = str. match (/^ (\ d {}) (-| \/) (\ d {}) \ 2 (\ d {}) $ /);
If (value = null ){
Return false;
}
Else {
Var date = new Date (value [1], value [3]-1, value [4]);
Return (date. getFullYear () = value [1] & (date. getMonth () + 1) = value [3] & date. getDate () = value [4]);
}
}
/**
* Check whether the time format is correct.
* Input: str string
* Return value: true or flase; true indicates that the format is correct.
* Verification time (10:57:10)
*/
Function checkTime (str ){
Var value = str. match (/^ (\ d {1, 2 })(:)? (\ D {1, 2}) \ 2 (\ d {1, 2}) $ /)
If (value = null ){
Return false;
}
Else {
If (value [1]> 24 | value [3]> 60 | value [4]> 60 ){
Return false
}
Else {
Return true;
}
}
}
/**
* Check whether the full Date and Time formats are correct
* Input: str string
* Return value: true or flase; true indicates that the format is correct.
* (10:57:10)
*/
Function checkFullTime (str ){
// Var value = str. match (/^ (\ d {}) (-| \/) (\ d {}) \ 2 (\ d }) :( \ d {1, 2}) :( \ d {1, 2}) $ /);
Var value = str. match (/^ (? : 19 | 20) [0-9] [0-9]-(? :(? : 0 [1-9]) | (? : 1 [0-2])-(? :(? : [0-2] [1-9]) | (? : [1-3] [0-1]) (? :(? : [0-2] [0-3]) | (? : [0-1] [0-9]): [0-5] [0-9]: [0-5] [0-9] $ /);
If (value = null ){
Return false;
}
Else {
// Var date = new Date (checkFullTime [1], checkFullTime [3]-1, checkFullTime [4], checkFullTime [5], checkFullTime [6], checkFullTime [7]);
// Return (date. getFullYear () = value [1] & (date. getMonth () + 1) = value [3] & date. getDate () = value [4] & date. getHours () = value [5] & date. getMinutes () = value [6] & date. getSeconds () = value [7]);
Return true;
}

}
 

/*************************************** **************************************** *******/
/************************************ ID card number verify *************************************/
/*************************************** **************************************** *******/
/**
* ID card 15-bit encoding rule: dddddd yymmdd xx p
* Dddddd: Location Code
* Yymmdd: Date of birth
* Xx: sequence class encoding, which cannot be determined
* P: gender. The odd number is male and the even number is female.
*


* ID card 18-bit encoding rule: dddddd yyyymmdd xxx y
* Dddddd: Location Code
* Yyyymmdd: Date of birth
* Xxx: sequence class code, which cannot be determined. The odd number is male and the even number is female.
* Y: Check Code. The value can be obtained through the first 17 digits.
*


* The 18-digit number weighting factor is (from right to left) Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1]
* Verification digit Y = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2]
* Formula for calculating the checkpoint: Y_P = mod (Σ (Ai × Wi), 11)
* I is the 2 .. 18 bits of the ID card number from right to left; Y_P is the position of the checkcode array where the ankle checkcode is located
*
*/
Var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1]; // Weighting Factor
Var ValideCode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2]; // ID card verification bid value. 10 represents X
Function IdCardValidate (idCard ){
IdCard = trim (idCard. replace (// g ,""));
If (idCard. length = 15 ){
Return isValidityBrithBy15IdCard (idCard );
}
Else
If (idCard. length = 18 ){
Var a_idCard = idCard. split (""); // obtain the ID card Array
If (isValidityBrithBy18IdCard (idCard) & isTrueValidateCodeBy18IdCard (a_idCard )){
Return true;
}
Else {
Return false;
}
}
Else {
Return false;
}
}
/**
* Check whether the last verification digit is correct when the ID card number is 18
* @ Param a_idCard ID card number Array
* @ Return
*/
Function isTrueValidateCodeBy18IdCard (a_idCard ){
Var sum = 0; // declare the weighted sum variable
If (a_idCard [17]. toLowerCase () = 'X '){
A_idCard [17] = 10; // Replace the last x verification code with 10 for subsequent operations
}
For (var I = 0; I <17; I ++ ){
Sum + = Wi [I] * a_idCard [I]; // weighted sum
}
ValCodePosition = sum % 11; // the location where the verification code is obtained
If (a_idCard [17] = ValideCode [valCodePosition]) {
Return true;
}
Else {
Return false;
}
}
/**
* Identify a male or female by ID card
* @ Param idCard: 15/18-digit ID card number
* @ Return 'female '-female, 'male'-male
*/
Function maleOrFemalByIdCard (idCard ){
IdCard = trim (idCard. replace (// g, ""); // process the ID card number. Contains spaces between characters.
If (idCard. length = 15 ){
If (idCard. substring (14, 15) % 2 = 0 ){
Return 'female ';
}
Else {
Return 'male ';
}
}
Else
If (idCard. length = 18 ){
If (idCard. substring (14, 17) % 2 = 0 ){
Return 'female ';
}
Else {
Return 'male ';
}
}
Else {
Return null;
}
}
/**
* Verify whether the birthday in the 18-digit ID card number is a valid birthday.
* @ Param idCard 18-digit book id card string
* @ Return
*/
Function isValidityBrithBy18IdCard (idCard18 ){
Var year = idCard18.substring (6, 10 );
Var month = idCard18.substring (10, 12 );
Var day = idCard18.substring (12, 14 );
Var temp_date = new Date (year, parseFloat (month)-1, parseFloat (day ));
// GetFullYear () is used to obtain the year to avoid the millennium bug.
If (temp_date.getFullYear ()! = ParseFloat (year) |
Temp_date.getMonth ()! = ParseFloat (month)-1 |
Temp_date.getDate ()! = ParseFloat (day )){
Return false;
}
Else {
Return true;
}
}
/**
* Verify that the birthday in the 15-digit ID card number is a valid birthday.
* @ Param idCard15 character book id card string
* @ Return
*/
Function isValidityBrithBy15IdCard (idCard15 ){
Var year = idCard15.substring (6, 8 );
Var month = idCard15.substring (8, 10 );
Var day = idCard15.substring (10, 12 );
Var temp_date = new Date (year, parseFloat (month)-1, parseFloat (day ));
// Use the getYear () method if you are older than your ID card.
If (temp_date.getYear ()! = ParseFloat (year) |
Temp_date.getMonth ()! = ParseFloat (month)-1 |
Temp_date.getDate ()! = ParseFloat (day )){
Return false;
}
Else {
Return true;
}
}
// Remove the leading and trailing spaces of the string
Function trim (str ){
Return str. replace (/(^ \ s *) | (\ s * $)/g ,"");
}

Author: "Computer Encyclopedia (only used for technology .."

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.