javascript實現檢驗的各種規則,javascript檢驗

來源:互聯網
上載者:User

javascript實現檢驗的各種規則,javascript檢驗

本文執行個體講述了javascript實現檢驗的各種規則。分享給大家供大家參考。具體如下:

/** * 檢驗各種規則 * @param str 檢驗的內容 * @param cType 預設的檢驗規則 字串[ *       empty,  檢驗是否為空白 *       telphone, 有線電話手機號碼 *       allphone, 所有手機號碼 *       ydphone, 移動手機號碼 *       ltphone, 聯通手機號碼 *       dxphone, 電信手機號碼 *       email,  郵箱 *       url,  網址 *       cn,   漢字 *       image,  圖片格式 *       emscode, 郵遞區號 *       isEmpty, 檢查是否為空白 *       isint,  整數 *       isfloat, 判斷是否為正小數 *       isnumber, 判斷為實數 *       words,  判斷是否為英文字母 *       wordsAndNum,   判斷是否為字母+數字 *       wordsAndNumAndDownline, 判斷是否由數字、26個英文字母或者底線組成的字串 *       qq,      QQ檢驗 *       personCard18,   身份證18位 *       personCard15,   身份證15位 *       ] * @param regex 自訂運算式 傳入格式例如:"^\-?[1-9]+\d*$" * * @description cType 與 regex 只能有一個為空白 *    如 checkObjectByRegex("測試中文", "cn"); // 判斷中文 *    如 checkObjectByRegex("測試中文", null, "^[\u4e00-\u9fa5]+$"); // 自訂運算式正則 * @return {boolean} */function checkObjectByRegex(str, cType, regex) { /**  * 定義驗證各種格式類型的Regex對象  */ var Regexs = {  telphone: (/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/), //有線電話手機號碼  allphone: (/^((13[0-9])|(14[57])|(15[0-9])|(17[678])|(18[0-9]))[0-9]{8}$/),   //所有手機號碼  ydphone: (/^((13[4-9])|(15[012789])|147|178|(18[23478]))[0-9]{8}$/),   //移動手機號碼  ltphone: (/^((13[0-2])|(145)|(15[56])|(176)|(18[56]))[0-9]{8}$/),     //聯通手機號碼  dxphone: (/^((133)|(153)|(177)|(180)|(181)|(189))[0-9]{8}$/),    //電信手機號碼  email: (/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/),//郵箱  url: (/(?:https|http|ftp|rtsp|mms):\/\/.+\/[\w]+\.[\w]+/),     //網址  cn: (/^[\u4e00-\u9fa5]+$/i), //漢字  image: (/\.jpg$|\.jpeg$|\.png$/i), //圖片格式  emscode: (/^[1-9]\d{5}$/), //郵遞區號  isint: (/^(\-)?[1-9]+\d*$/), //整數  isfloat: (/^[0-9]+\.?[0-9]*$/), //判斷是否為正小數  isnumber: (/^[-\+]?\d+(\.\d+)?$/), //判斷為實數  words: (/^[A-Za-z]+$/), //判斷是否為英文字母  wordsAndNum: (/^[A-Za-z0-9]+$/), //判斷是否為字母+數字  wordsAndNumAndDownline: (/^\w+$/), //判斷是否由數字、26個英文字母或者底線組成的字串  qq: (/^[1-9]\d{4,11}$/), //QQ  personCard18: (/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\d|X)$/), //身份證18位  personCard15: (/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/) //身份證15位 }; 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; //輸入為空白,認為是驗證通過  // 針對 18位身份證單獨處理  if (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;  }  // 針對 15位身份證單獨處理  if (cType == 'personCard15') {   var ary = str.match(Regexs[cType]);   var D = new Date("19" + ary[3] + "/" + ary[4] + "/" + ary[5]);   var isTrue = D.getYear() == ary[3] && (D.getMonth() + 1) == ary[4] && D.getDate() == ary[5];   return isTrue;  } } else {  // 自訂Regex處理  if (regex != null && typeof(regex) != "undefined") {   nReg = new RegExp(regex);  } else {   return false;  } } return nReg.test(str);}

希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.