Complete individual JS script verification [go]

Source: Internet
Author: User
/**
* 2009-10-01
* He Chen
* Sentiment
* JavaScript form data verification
*/
/*************************************** **************************************** *******/
/************************************ 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;
}
}

/*************************************** **************************************** *******/
/************************************ 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
* enter: STR string
* return: true or flase; true indicates 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 for 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;
}< br> else {
return true;
}< BR >}

/**
* check whether the Entered IP address is correct
* enter: STR string
* return: true or flase; true indicates the format is correct
*/
function checkip (STR) {
var Arg =/^ (\ D {1, 2} | 1 \ D \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ D {1, 2} | 1 \ D \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ D {1, 2} | 1 \ D \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ D {1, 2} | 1 \ D \ d | 2 [0-4] \ d | 25 [0-5]) $/;
If (Str. match (ARG) = NULL) {
return false;
}< br> else {
return true;
}< BR >}

/**
* 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;
}
}

/*************************************** **************************************** *******/
/************************************ 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.
* <P/>
* 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.
* <P/>
* 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 verification code with the last digit X with 10 for subsequent operations
}< br> for (VAR I = 0; I <17; I ++) {
sum + = wi [I] * a_idcard [I]; // weighted sum
}< br> valcodeposition = sum % 11; // obtain the location where the verification code is located
If (a_idcard [17] = validecode [valcodeposition]) {
return true;
}< br> else {
return false;
}< BR >}

/**
* 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 here. 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;
}< br> else {
return true;
}< BR >}

/**
* verify whether the birthday in the 15-digit ID card number is a valid birthday
* @ Param idcard15 15-digit 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);
// you do not need to consider the Millennium for your age in the old ID card If (temp_date.getyear ()! = Parsefloat (year) |
temp_date.getmonth ()! = Parsefloat (month)-1 |
temp_date.getdate ()! = Parsefloat (day) {
return false;
}< br> else {
return true;
}< BR >}

// remove the leading and trailing spaces of the string
function trim (STR) {
return Str. replace (/(^ \ s *) | (\ s * $)/g, "");
}

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.