本文給大家總結了一下常見的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) {
- /**
- * 定義驗證各種格式類型的正則表達式對象
- */
- 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 {
- // 自定義正則表達式處理
- if (regex != null && typeof(regex) != "undefined") {
- nReg = new RegExp(regex);
- } else {
- return false;
- }
- }
- return nReg.test(str);
- }
以上所述就是本文的全部內容了,希望大家能夠喜歡。
新聞熱點
疑難解答