Dry (Classic) _javascript Tips for practical JavaScript validation and data processing at work

Source: Internet
Author: User
Tags datetime getdate

In the development of Web projects, it is unavoidable to encounter a variety of Web page data processing, such as the user in the form of the phone number, mailbox, amount, ID number, password length and complexity, and so on the verification, as well as the background return data such as the amount of money, the return value is null There is also the calculation of a day or a January before or after the specified date.

Meet the need for data and form verification, I believe that everyone like me, like to find relevant methods on the Internet, because they write, is more time-consuming. Today I would like to share with you some of the work of the summary of some of the commonly used
Of JS.

The key code looks like this:

/** * Verify password complexity (must contain numeric letters) * @param str * @returns true: satisfies the rule, false: */function Validatestr (str) {var reg1 =/^ ([0 -9]{1,}) ([A-z]{1,})] | 
([A-z]{1,}) ([0-9]{1,})] $/; var reg2 =/^ ([0-9]{1,}) ([A-z]{1,}) | 
([A-z]{1,}) ([0-9]{1,})] $/; var reg3 =/^ ([a-za-z]{0,}) [0-9a-z-a-z]{0,}[~ '!@#$%^&*.] 
{0,}$/; 
str = Valuetrim (str); 
if (Reg3.test (str)) {//return true; 
} if (Reg1.test (str)) {return true; 
} if (Reg2.test (str)) {return true; 
return false; /** * Judge string length must be greater than 8 bits or less than 20 bits, generally used for password * @param str string * @returns satisfy return true */function validatelength (str) {if str==nu ll | | 
str== ') {return false; 
str = Valuetrim (str); 
if (parsefloat (str.length) <8) {return false; 
} if (Parsefloat (str.length) >20) {return false; 
return true; /** * Verification Time * @param datavalue format is: YYYY-MM-DD * @returns match return True mismatch return false/function ValiDate (dateValue) {var result = Datevalue.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?) [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) $)) 
/); 
if (Result==null) {return false; 
return true; 
/** * Verify Phone number * @param phonevalue the phone number to be validated * @returns match returns True mismatch return false/function Validatephone (phonevalue) { 
Phonevalue = Valuetrim (Phonevalue); 
var reg =/^[1][0-9]{10}$/; 
Return Reg.test (Phonevalue); /** * Verify Mailbox * @param emailvalue to verify mailbox * @returns Match returns True mismatch return false/function Validateemail (emailvalue) {var r eg =/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] 
\w+) *$/; 
Return Reg.test (Emailvalue); /** * Determine if it is a number * @param numbervalue the data to be validated * @returns match returns TrueWith return false/function Isnumber (numbervalue) {//define regular expression part of var reg1 =/^[0-9]{0,}$/; 
var reg2 =/^[1-9]{1}[0-9]{0,}$/; 
alert (Numbervalue); 
if (numbervalue ==null | | | numbervalue.length==0) {return false; 
} Numbervalue = Valuetrim (Numbervalue); 
To determine if the number is only 1-bit if (numbervalue.length<2) {return reg1.test (numbervalue); 
Return Reg2.test (numbervalue);; 
/*** * Amount * @param value * @returns/function Ismoney (value) {if (value== ') {return false; 
} value = Valuetrim (value); Value = Value.replace (/(^\s*) | ( 
\s*$)/g, ""); var reg =/^[0-9]*\.? [0-9] 
{0,2}$/; 
if (Isnumber (value)) {return true; 
} if (value.length>3) {if (value.substr (0, 1) = = "0") {if (Value.substr (3,value.length). length>2) {return false; 
}} return Reg.test (value); 
/*** * To determine whether a number between 0 and 100 * @param value * @returns/function Iszerotoone (value) {if (value== ') {return false; 
} value = Valuetrim (value); if (ismyfloat (value)) {if parsefloat (value) <100 && parsefloat (value)>0) {return true; 
return false; /** * Verify is floating-point number * @param floatvalue data to validate * @returns match returns True mismatch return false/function Ismyfloat (floatvalue) {if FL 
oatvalue== ') {return false; 
} Floatvalue = Valuetrim (Floatvalue); 
var reg =/^ (\d+) (\.\d+) $/; 
if (Isnumber (Floatvalue)) {return true; } if (floatvalue.length>3) {if (floatvalue.substr (0, 1) = = "0") {if (Floatvalue.substr (0, 2)!= "0.") 
{return false; 
}} return Reg.test (Floatvalue); /** * Determine if it is Kanji * @param charvalue data to validate * @returns match returns True mismatch return false/function Ischaracter (charvalue) {var re 
g =/^[\u4e00-\u9fa5]{0,}$/; 
Return Reg.test (Charvalue);  /** * Verify the landline number * @param telvalue to verify the landline number * @returns match returns True mismatch return false/function Validatetel (telvalue) {var reg = 
/^ (\d{3,4}\) |\d{3,4}-)? \d{7,8}$/; 
Telvalue = Valuetrim (Telvalue); 
if (!reg.test (Telvalue)) {return false; 
return true; var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1]; Weighted factor var validecode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2]; ID card Verification bit value. 10 represents x/** * Authentication ID * @param idcard need to verify ID number * @returns match return True mismatch return false/function idcardvalidate (IdC 
Ardvalue) {///Remove string-and-tail space idcardvalue = Valuetrim (idcardvalue.replace (//g, "")); 
if (idcardvalue.length = = 15) {//Authenticate the 15-bit ID card return Isvaliditybrithby15idcard (Idcardvalue); 
else if (idcardvalue.length = 18) {//Get identity card array var a_idcard = Idcardvalue.split (""); Basic authentication for 18-bit IDs and 18th-bit authentication if (Isvaliditybrithby18idcard (idcardvalue) &&istruevalidatecodeby18idcard (A_ 
Idcard)) {return true; 
}else {return false; 
} else {return false; }/** * Determines whether the final verification bit is correct with the ID number 18 digits * @param a_idcard ID Number array * @return/function Istruevalidatecodeby18idcard (A_IDC 
ard) {var sum = 0;//Declare the weighted sum variable if (a_idcard[17].tolowercase () = = ' x ') {a_idcard[17] = 10;//Replace the last bit X's captcha with 10 for subsequent operations} for (var i = 0; i < i++) {sum + + wi[i] * a_idcard[i];//weighted sum} valcodeposition = sum% 11; Get the location of the captcha if (a_idcard[17] = = validecode[Valcodeposition]) {return true; 
else {return false; }/** * Verify that the birthday in the 18-digit ID number is a valid birthday * @param idcard 18-digit ID card String * @return/function Isvaliditybrithby18idcard (idCard1 
8) {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)); Use getFullYear () to get the year to avoid the bug problem if (Temp_date.getfullyear ()!=parsefloat () | | 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 number is a valid birthday * @param idCard15 15-digit ID card String * @return/function Isvaliditybrithby15idcard (idcar 
D15) {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)); The age of your old ID card does not need to consider the millennium bug problem and use the getyear () method if (Temp_date.getyear ()!=parsefloat (year) | | Temp_date.getmonth ()!=PARSEFLOat (month)-1 | | 
Temp_date.getdate ()!=parsefloat (Day)} {return false; 
}else{return true; }///Remove string-and-tail space function Valuetrim (str) {return str.replace (^\s*) | ( 
\s*$)/g, ""); /** * Test 18 ID Number (15-digit number can only detect the birthday is correct, self-resolved) * @param idcardvalue 18-digit ID number * @returns match return True mismatch return false/function  Idcardvildate (CID) {var arrexp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];//weighted factor var arrvalid = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];//check code var reg =/^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2])) (([0|1|2]\d) |3[0-1]) \d{3} ([0-9]| 
X) $/; 
if (Reg.test (CID)) {var sum = 0, idx; 
for (var i = 0; i < cid.length-1 i++) {//pair of first 17 digits with weight product sum sum + = parseint (Cid.substr (i, 1), ten) * arrexp[i]; 
///calculation mode (fixed algorithm) idx = sum% 11; 
Check whether the 18th is equal to check code return ARRVALID[IDX] = = CID.SUBSTR (1). toUpperCase (); 
}else{return false; }/** * Gets the day before or after the specified date * * @param daycount * Positive number is a later time, negative number for the previous time such as: 1 for Tomorrow, 1 for yesterday * */function getdatestr (dates, DA Ycount) {var dateTime = Daycount * 24 * 60 * 60 * 1000; 
var dd = new Date (); 
if (dates = = "") {dd = new Date (); 
else {dd = new Date (dates); 
var datenumber = dd.gettime () + dateTime; 
var newdate = new Date (datenumber); 
var y = newdate.getfullyear (); 
var m = newdate.getmonth () + 1;//Gets the date of the current month var d = newdate.getdate (); 
if (M < ten) {m = "0" + m; 
} if (d < ten) {d = "0" + D; 
Return y + "-" + M + "-" + D; /** * Gets the month before or after the specified month * * @param daycount * Positive number is the following month, minus is the previous month such as: 1 for the next month,-1 for the last month */function getmonthstr (dates, MO 
Nthcount) {var dd = new Date (); 
if (dates = = "") {dd = new Date (); 
else {dd = new Date (dates); 
var y = dd.getfullyear (); 
var m = dd.getmonth () + 1;//Gets the date of the current month m = m + monthcount; 
if (M = = 0) {m = "12"; 
y = y-1; 
else if (M < ten) {m = "0" + m; 
else if (M >) {m = m-12; 
m = "0" + m; 
y = y + 1; 
Return y + "-" + M; 
/** * * For Val value undefined return "", otherwise return the original value */function Dealnull (val) {if (typeof (val) = = "undefined") {""; } else {return val;  } 
}

The above is a small set to introduce the work of more practical JavaScript verification and data processing of dry Goods (classic), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.