Common JS library functions

Source: Internet
Author: User
Source http://www.dotblogs.com.tw/newmonkey48/archive/2009/11/22/12104.aspx
JS Code website http://www.cftea.com/c/2006/08/1F94458YM61AMFVD.asp
// JScript file // ******************** Add the composer JavaScript function ***** // string. prototype. trim = function () {return this. replace (/(^ \ s *) | (\ s * $)/g, "");} string. prototype. ltrim = function () {return this. replace (/(^ \ s *)/g, "");} string. prototype. rtrim = function () {return this. replace (/(\ s * $)/g ,"");} // ******************* certificate ****************** * //// function checklen (controlid, length, fielddesc, blnreque St) {var control = getcontrol (controlid); VaR value = getcontrolvalue (controlid); var stralert = ""; if (blnrequest = true) {stralert = checkcontrol ("request", controlid, fielddesc); If (stralert! = "") Return stralert;} else if (value = "") Return ""; if (blen (value)> length) {return fielddesc + "the number of characters in a field cannot exceed" + (length/2) + "the number of words or English characters cannot exceed" + Length +! \ R \ n ";} return" ";}// returns the length function blen (STR) {// exam site // http://www.360doc.com/content/081222/15/16915_2177384.html var Len = Str. match (/[^-~] /G) = NULL? Str. Length: Str. Length + Str. Match (/[^-~] /G ). length; return Len;} // determines whether the start time of the period is greater than the end time function checkdaterange (begdateid, enddateid, fielddesc, blnrequest) {var objbegdate; var objenddate; vaR valuebegdate, valueenddate; objbegdate = getcontrol (begdateid); objenddate = getcontrol (enddateid); valuebegdate = getcontrolvalue (begdateid); valueenddate = getcontrolvalue (enddateid); region = date. parse (objbegdate. value); dtenddate = date. parse (objenddat E. Value); If (blnrequest = true) {stralert = checkcontrol ("request", begdateid, fielddesc + "start date"); If (stralert! = "") Return stralert; stralert = checkcontrol ("request", enddateid, fielddesc + "end date"); If (stralert! = "") Return stralert;} else if (valuebegdate = "" | valueenddate) Return ""; if (dtbegdate> dtenddate) {return "date large, it cannot be placed in front! \ R \ n ";}return" ;}// determines whether the period is in the date format of function checkdate (controlid, fielddesc, blnrequest) {var control = getcontrol (controlid ); vaR value = getcontrolvalue (controlid); If (blnrequest = true) {stralert = checkcontrol ("request", controlid, fielddesc); If (stralert! = "") Return stralert;} else if (value = "") Return ""; // rules: yyyy/mm/DD var OBJ, strvalue, flag = false; strvalue = getcontrolvalue (controlid); If (strvalue = "") Return ""; vaR objregexp =/^ \ D {4} \/\ D {1, 2} \/\ D {1, 2} $/; // check to see if in correct format if (! Objregexp. Test (strvalue) {return fielddesc + "the digit must be in the date format of the western dollar! (Yyyy/mm/DD) \ r \ n "; // doesn't match pattern, bad date} else {var strseparator = '/'; var arraydate = strvalue. split (strseparator); // create a lookup for months not equal to Feb. vaR arraylookup = {'01 ': 31, '03': 31, '04': 30, '05 ': 31, '06': 30, '07': 31, '08 ': 31, '09': 30, '10': 31, '11': 30, '12': 31} var intday = parseint (arraydate [2], 10); // check if month value and day value agree if (Ar Raylookup [arraydate [1]! = NULL) {If (intday <= arraylookup [arraydate [1] & intday! = 0) Flag = true; // found in lookup table, good date} // check for February (bugfix 20050322) // bugfix for parseint Kevin/bugfix Biss year o. JP voutat var intmonth = parseint (arraydate [1], 10); If (intmonth = 2) {var intyear = parseint (arraydate [0]); if (intday> 0 & intday <29) {flag = true;} else if (intday = 29) {If (intyear % 4 = 0) & (intyear % 100! = 0) | (intyear % 400 = 0) {// year Div by 4 and (not Div by 100) or Div by 400) -> OK flag = true ;}}}if (flag = false) {return fielddesc + "the specified date information has been updated! \ R \ n ";}return" ;}// determine whether the value is in the digital format of function checknumber (controlid, fielddesc, blnrequest) {var control = getcontrol (controlid ); vaR value = getcontrolvalue (controlid); If (blnrequest = true) {stralert = checkcontrol ("request", controlid, fielddesc); If (stralert! = "") Return stralert;} else if (value = "") Return ""; if (isnumber (value) = false) {return fielddesc + "the digit must be a number! \ R \ n ";}return" ";}// determines whether the entire function checkinteger (controlid, fielddesc, blnrequest) {var control = getcontrol (controlid ); vaR value = getcontrolvalue (controlid); If (blnrequest = true) {stralert = checkcontrol ("request", controlid, fielddesc); If (stralert! = "") Return stralert;} else if (value = "") Return ""; if (isinteger (value) = false) {return fielddesc + "the bitwise must be an integer! \ R \ n ";}return" ";}// determines whether the receiver is in the email format of function checkemail (controlid, fielddesc, blnrequest) {var control = getcontrol (controlid ); vaR value = getcontrolvalue (controlid); If (blnrequest = true) {stralert = checkcontrol ("request", controlid, fielddesc); If (stralert! = "") Return stralert;} else if (value = "") Return ""; if (isemail (value) = false) {return fielddesc + "the signature must be email! \ R \ n ";}return" ";}// determine whether the role is in the function checkidnumber (controlid, fielddesc, blnrequest) format) {var control = getcontrol (controlid); VaR value = getcontrolvalue (controlid); If (blnrequest = true) {stralert = checkcontrol ("request", controlid, fielddesc ); if (stralert! = "") Return stralert;} else if (value = "") Return ""; if (isidnumber (value) = false) {return fielddesc + "signature must be in the authentication token format! \ R \ n ";} return" ";}// confirm whether the input information function checkrequest (controlid, fielddesc) {var control = getcontrol (controlid ); vaR value = getcontrolvalue (controlid); var tagname = control. tagname. tolowercase (); If (tagname = "select") {If (control. options [control. selectedindex]. value = "") {return "Please select" + fielddesc + "\ r \ n";} else return "";} else if (tagname = "input ") {var type = control. typeswitch (type) {Case "text": If (isempty (Value) {return "" + fielddesc + "\ r \ n";} break; Case "checkbox": break; Case "radio": break ;}} return "";} // ********************************* shared method ***** **************************** // obtain the control function getcontrolvalue (controlid) {var control; VaR value; control = getcontrol (controlid); value = control. value; Return Value} // obtain the value of the control counter function getcontrol (controlid) {return document. getelementbyid (controlid);} // simplified version Required parameter functionfunction checkcontrol (type, controlid, fielddesc, blnrequest) {Switch (type) {Case "date": Return checkdate (controlid, fielddesc, blnrequest); break; case "Number": Return checknumber (controlid, fielddesc, blnrequest); break; Case "integer": Return checkinteger (controlid, fielddesc, blnrequest); break; Case "email ": return checkemail (controlid, fielddesc, blnrequest); break; Case "idnumber": Return checkidnumbe R (controlid, fielddesc, blnrequest); break; Case "request": Return checkrequest (controlid, fielddesc, blnrequest); break ;}} // set function controlfocus (controlid) {var control = getcontrol (controlid); control. focus ();} // ******************************** basic judgment function *** *********************** // determine whether the value is a function isnumber (value) {If (isnan (value) return false; return true;} // judge whether the value is an integer function isinteger (value) {v Ar objregexp =/^ -? \ D + $/; If (! Objregexp. test (value) return false; return true;} // you must have a function isempty (value) {If (value. trim () = "") return true; else return false;} // determines whether the callback is e-mailfunction isemail (value) {var objregexp =/^. + @. + \.. + $/; If (! Objregexp. test (value) return false; return true;} // determines whether the token is an ID function isidnumber (value) {// sorted by the letter delimiter, used to store the values column. VaR letters = new array ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'h ', 'J', 'k', 'l', 'M', 'n', 'P', 'Q', 'R', 's','t ', 'U', 'V', 'x', 'y', 'w', 'z', 'I', 'O '); // store the multiplication numbers var multiply = new array (1, 9, 8, 7, 6, 5, 4, 3, 2, 1 ); vaR Nums = new array (2); var firstchar; var firstnum; var lastnum; var Total = 0; // create a regular expression 」. The first word is an English letter, // The second word is 1 or 2, followed by 8 numbers, regardless of size. VaR regexpid =/^ [A-Z] (1 | 2) \ D {8} $/I; // use the regular expression of the Regular Expression if (value. search (regexpid) =-1) {// basic format: Invalid Response Return false;} else {// retrieve the first element and the last digit. Firstchar = value. charat (0 ). touppercase (); lastnum = value. charat (9) ;}// locate the corresponding number of the first letter and convert it to two numbers. For (VAR I = 0; I <26; I ++) {If (firstchar = letters [I]) {firstnum = I + 10; Nums [0] = math. floor (firstnum/10); Nums [1] = firstnum-(Nums [0] * 10); break ;}} // compute the sum of rows for (VAR I = 0; I <multiply. length; I ++) {if (I <2) {total + = Nums [I] * multiply [I];} else {total + = parseint (value. charat (I-1) * multiply [I];} // compare to the last numeric value if (10-(Total % 10 ))! = Lastnum) {return false;} return true ;}

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.