JavaScript Common validation Function summary _javascript skill

Source: Internet
Author: User
Tags getdate
/** * 2010-7-13 * He Sen * Love * js various forms of data validation * * */*********************************************************************** //************************************* Digital Verification *****************************************//********* //** * Check whether a string of characters entered is all numeric * input: str String * return Back: TRUE or flase;


True is the number/function Checknum (str) {return Str.match (/\d/) = = null;} /** * Check whether a string of characters entered is a decimal * input: STR String * Returns: TRUE or Flase;
  True is decimal */function Checkdecimal (str) {if (Str.match/^-?\d+ (\.\d+) $/g) = null) {return false;
  else {return true; }/** * Checks whether a string of characters entered is an integer data * input: STR String * Returns: TRUE or Flase;
  True is decimal */function Checkinteger (str) {if (Str.match (/^[-+]?\d*$/) = = null) {return false;
  else {return true; }
}

/**************************************************************************************/
/******************** Character Verification *****************************************/
/**************************************************************************************/


/**
 * Check if a string of characters entered is a character * input: str String * Returns: TRUE or Flase;
  True indicates that all characters do not contain kanji */function checkstr (str) {if (/[^\x00-\xff]/g.test (str)) {return false;
  else {return true; }/** * Check that a string of characters entered contains kanji * Input: str String * Returns: TRUE or Flase;
  True to include kanji/function Checkchinese (str) {if (Escape (str). IndexOf ("%u")!=-1) {return true;
  else {return false; }/** * Check that the entered mailbox format is correct * ENTER: STR String * Returns: TRUE or Flase; True indicates a well-formed/function Checkemail (str) {if Str.match (/[a-za-z0-9_-]+[@) (\s*) (net|com|cn|org|cc|tv|[
  0-9]{1,3}) (\s*)/g) = = null) {return false;
  else {return true; }/** * Check the format of the phone number entered is correct * ENTER: STR String * return: TRUE or flase; True indicates well-formed/function Checkmobilephone (str) {if (Str.match (/^ (?: 13\d|15[89))-?\d{5} (\d{3}|\*{3}) $/) = = null) {RET
  Urn false;
  else {return true; }/** * Check that the fixed phone number entered is correct *Input: str String * Returns: TRUE or Flase; True represents a well-formed/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 QQ format is correct * ENTER: STR String * return: TRUE or flase;
  True indicates a well-formed/function checkqq (str) {if str.match (/^\d{5,10}$/) = = null) {return false;
  else {return true; }/** * Check that the ID number entered is correct * ENTER: STR String * Returns: TRUE or Flase; True indicates a well-formed/function Checkcard (str) {//15-digit ID card regular expression var arg1 =/^[1-9]\d{7} ((0\d) | (
  1[0-2])) (([0|1|2]\d) |3[0-1]) \d{3}$/; 18-digit ID card regular expression 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 that the IP address entered is correct * ENTER: STR String * Returns: TRUE or Flase; True indicates a well-formed/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;
  else {return true; }/** * Check that the URL address entered is correct * ENTER: STR String * Returns: TRUE or Flase; True indicates a well-formed/function Checkurl (str) {if Str.match (/http[s]?| FTP): \/\/[^\/\.] +?\..
  +\w$/i = = null) {return false} else {return true; }/** * Check if the character entered has a special character * input: str String * Returns: TRUE or Flase; True indicates that a special character * is used primarily for registration information validation/function Checkquote (str) {var items = new Array ("~", "'", "!", "@", "#", "$", "%", "^")
  , "&", "*", "{", "}", "[", "]", "(", ")");
  Items.push (":", ";", "'", "|", "\", "<", ">", "?", "/", "<<", ">>", "| |", "//");
  Items.push ("admin", "Administrators", "Administrator", "admin", "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; }


/**************************************************************************************/
/********************* Verification of Time *****************************************//*********************************************** //** * Check date format is correct * ENTER: STR String * return: TRUE or flase; True indicates the format is correct * Note: The Chinese date format cannot be validated here * Verify Short Date (2007-06-05)/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?) [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{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) $/);
  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 that the time format is correct * ENTER: STR String * Returns: TRUE or Flase; True means 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] > | | value[3] > [| | value[4] >) {return false} else {R
    Eturn true; }}/** * Check full date time format is correct * ENTER: STR String * Returns: TRUE or Flase; True indicates a well-formed * (2007-06-05 10:57:10)/function Checkfulltime (str) {//var value = Str.match (/^ (\d{1,4}) (-|\/) (\d{1,2}) \
  2 (\d{1,2}) (\d{1,2}):(\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], checkfull
    TIME[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; }
  
}




/**************************************************************************************/
/******************** Verification of ID number *************************************//************************************************ //** * ID card 15-bit encoding rule: dddddd YYMMDD xx p * DDDDDD: Region code * YYMMDD: Birth date * XX: Sequential class Code , unable to determine * p: sex, odd male, even number for female * <p/> * ID card 18-bit encoding rule: dddddd yyyymmdd xxx y * dddddd: Region code * YYYYMMDD: Birth date * XXX: Sequential class code, Cannot determine, odd number is male, even number isFemale * y: Parity code, which can be computed by the first 17 digits, * <p/> * 18-digit weighting factor (from right to left) Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1] * Verify bit Y = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2] * check digit calculation formula: y_p = mod (∑ (AIXWI), one) * I for the ID number from the right to the left of the number of 2...18 bit;  Y_p for Feet Check Code array location */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 Verification bit value. 10 represents X function Idcardvalidate (idcard) {Idcard = Trim (idcard.replace (//g, "
  "));
  if (idcard.length = =) {return Isvaliditybrithby15idcard (Idcard); else if (idcard.length = =) {var A_idcard = Idcard.split ("");//Get an ID array if (isvaliditybrithby18i
      Dcard (Idcard) && 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_IDCA RD) {var sum = 0;  Declares the weighted sum variable if (a_idcard[17].tolowercase () = = ' x ') {a_idcard[17] = 10;//replaces the last bit X's captcha with 10 for subsequent operations} for (Var i = 0; I < 17; i++) {sum + = wi[i] * a_idcard[i];//weighted sum} valcodeposition = sum% 11;//get captcha position if (a_idcard[17) = = Valid
  Ecode[valcodeposition]) {return true;
  else {return false; }/** * Through the identity card is a male or female * @param idcard 15/18 ID Card number * @return ' female '-female, ' male '-male * * Function Maleorfemalbyidcard (IDCA RD) {Idcard = Trim (idcard.replace (//g, "")), and/or handle the ID number.  
  Includes spaces between characters.
    if (Idcard.length =) {if (idcard.substring)% 2 = 0) {return ' female ';
    else {return ' male ';
      ' Else if (idcard.length = = {if (idcard.substring)% 2 = 0) {return ' female ';
      else {return ' male ';
    } else {return null; }/** * Verify that the birthday in the 18-digit ID number is a valid birthday * @param idcard 18-digit 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));
  Here 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 (idcard
  {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 trim (str) {return str.replace (/^\s*) | ( \s*$)/g, "");

This is the entire article, learn more about JavaScript syntax, you can see: "JavaScript Reference Tutorial," JavaScript Code style guide, and also hope that you support the cloud-Habitat community.

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.