JavaScript Common Regular expression Rollup _javascript tips

Source: Internet
Author: User

JavaScript-Common regular expression totals

/** * Inspection of various rules * @param str Test content * @param cType default inspection rule string [* empty, check for empty * telphone, block Machine cell phone number * Allphone, all mobile phone number * Ydphone, mobile phone number * Ltphone, Unicom mobile phone number * DXPH
 One, mobile phone number * email, email * URL, URL * cn, Chinese Characters * image, picture format 
 * Emscode, ZIP code * isempty, check for null * isint, Integer * isfloat, determine if positive decimal             * Isnumber, judged as the real number * words, to determine whether it is the English letter * Wordsandnum, to determine whether the letter + numbers *      Wordsandnumanddownline, to determine whether by number, 26 English letters or underscores of the string * QQ, QQ Test * personCard18, ID 18 * PERSONCARD15, ID 15 *] * @param regex custom expression incoming format for example: "^\-?"  [1-9]+\d*$ * * * @description CType and regex can only have one for empty * such as Checkobjectbyregex ("Test Chinese", "cn"); Judge Chinese * such as Checkobjectbyregex ("Test Chinese", NULL, "^[\u4e00-\u9fa5]+$");
  Custom expression Regular * @return {Boolean}/function Checkobjectbyregex (str, cType, regex) {/** * Defines regular expression objects that validate various format types * * var Regexs = {telphone: (/^ () (\d{3}\) | ( \d{3}\-))? (\ (0\d{2,3}\) |0\d{2,3}-)? [1-9]\d{6,7}$/),//Landline phone number allphone: (/^ (13[0-9) | ( 14[57]) | (15[0-9]) | (17[678]) | (18[0-9]) [0-9] {8}$/),//All mobile numbers Ydphone: (/^ (13[4-9) | ( 15[012789]) |147|178| (18[23478]) [0-9] {8}$/),//Mobile phone number ltphone: (/^ (13[0-2) | ( 145) | (15[56]) | (176) | (18[56]) [0-9] {8}$/),//Unicom mobile phone number dxphone: (/^ (133) | ( 153) | (177) | (180) | (181) | (189)) [0-9] {8}$/),//Telecom mobile phone number email: (/^ ([a-za-z0-9_-]) +@ ([a-za-z0-9_-]) + ((\.[ a-za-z0-9_-]{2,3}) ({1,2}) $/),//Mailbox URL: (/(?: Https|http|ftp|rtsp|mms): \/\/.+\/[\w]+\.[                         \w]+/),//URL cn: (/^[\u4e00-\u9fa5]+$/i),///Chinese character Image: (/\.jpg$|\.jpeg$|\.png$/i), Picture format Emscode: (/^[1-9]\d{5}$/),//Zip code isint: (/^ (\-)? [ 1-9]+\d*$/),//integer isfloat: (/^[0-9]+\.? [0-9]*$/),//judge whether positive decimal isnumber: (/^[-\+]?\d+ (\.\d+) $/),//judged as real number words: (/^[a-za-z]+ $/),//To determine whether the letter Wordsandnum: (/^[a-za-z0-9]+$/),//judge whether the letter + digital Wordsandnumanddo Wnline: (/^\w+$/),//judge whether a string consisting of a number, 26 English letters or underscores QQ: (/^[1-9]\d{4,11}$/),//qq per SonCard18: (/^ (\d{6}) ()? ( \D{4}) (\d{2}) (\d{2}) (\d{3}) (\d| X) $/),//ID 18-bit personCard15: (/^ (\d{6}) ()? (
  \D{2}) (\d{2}) (\d{2}) (\d{3}) $/)//ID 15-bit};
  var Nreg;
  if (str = null | | typeof (str) = = "undefined") {str = "";
      } if (CType!= null && typeof (CType)!= "undefined") {if (CType = "IsEmpty") {str = $.trim (str);
      if (str!= null && typeof (str)!= "undefined" && str!= "") {return false;
    else return true;
    } Nreg = Regexs[ctype]; if (str = NULL | | | str = = "") return false;
    The input is null and is considered to be authenticated through//for 18-bit ID card handling aloneif (CType = = ' PersonCard18 ') {var ary = Str.match (Regexs[ctype)); if (!) (
      parseint (Ary[3]) >= 1900)) return false;
      var D = new Date (ary[3] + "/" + ary[4] + "/" + ary[5]);
      var isTrue = d.getfullyear () = = Ary[3] && (D.getmonth () + 1) = = Ary[4] && d.getdate () = = Ary[5];
    return isTrue;
      ///For 15-bit ID card alone processing if (CType = = ' PersonCard15 ') {var ary = Str.match (Regexs[ctype));
      var D = new Date ("n" + ary[3] + "/" + ary[4] + "/" + ary[5]);
      var isTrue = d.getyear () = = Ary[3] && (D.getmonth () + 1) = = Ary[4] && d.getdate () = = Ary[5];
    return isTrue; } else {//custom regular expression processing if (Regex!= null && typeof (regex)!= "undefined") {Nreg = new RegExp (re
    Gex);
    else {return false;
} return Nreg.test (str);
 }

The above is the entire contents of this article, I hope you can enjoy.

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.