本文實(shí)例講述了JS常用正則表達(dá)式。分享給大家供大家參考,具體如下:
在項(xiàng)目中個(gè)人寫(xiě)的一個(gè)常用驗(yàn)證的正則表達(dá)式:(僅供參考)
//定義兩個(gè)全局變量var reg;var errorInfo;//輸入的數(shù)字類(lèi)型必須是int型,正負(fù)整數(shù)都可以function validate_integer(text,value){reg=/^[-/+]?/d+$/;errorInfo=text+" "+value+" 只能是整數(shù),請(qǐng)核實(shí)重新輸入!";verifyByReg(value,reg,errorInfo);}//輸入的數(shù)字類(lèi)型必須是double型,保留的小數(shù)位數(shù)只能是2位,可以為正負(fù)function validate_double(text,value){reg=/^[-/+]?([1-9](/d+)?|0)(/./d{2})$/;errorInfo=text+" "+value+" 只能保留2為小數(shù),請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//輸入的必須是全是數(shù)字類(lèi)型型,不能出現(xiàn)其他字符function validate_number(text,value){reg=/^/d+$/;errorInfo=text+" "+value+" 只能是數(shù)字,請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證輸入n位的數(shù)值類(lèi)型function validate_number_n(text,value,n){reg=eval("/^//d{"+n+"}$/");errorInfo=text+" "+value+" 只能是"+n+"位數(shù)字,請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證有兩位小數(shù)的正實(shí)數(shù)function validate_decimal_two(text,value){reg=/^([1-9](/d+)?|0)(.[0-9]{2})?$/;errorInfo=text+" "+value+" 只能是保留2位小數(shù)的正數(shù),請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證非零的正整數(shù)function validate_number_positive(text,value){reg=/^/+?[1-9][0-9]*$/;errorInfo=text+" "+value+" 只能是非0的正整數(shù),請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證非零的負(fù)整數(shù)function validate_number_negative(text,value){reg=/^[-][1-9][0-9]*$/;errorInfo=text+" "+value+" 只能是非0的負(fù)整數(shù),請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證非負(fù)整數(shù),可以為0function validate_positive_haszero(text,value){reg=/^[1-9](/d+)?|0$/;errorInfo=text+" "+value+" 只能是是0或正整數(shù),請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證非正整數(shù),可以為0function validate_negative_haszero(text,value){reg=/^/-[1-9](/d+)?|0$/;errorInfo=text+" "+value+" 只能是是0或負(fù)整數(shù),請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證非負(fù)浮點(diǎn)數(shù)(正浮點(diǎn)數(shù) + 0)function validate_float_notnegative(text,value){reg=/^([1-9](/d+)?|0)(/./d+)|0$/;errorInfo=text+" "+value+" 只能是是0或正浮點(diǎn)數(shù),請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證正浮點(diǎn)數(shù)function validate_float_positive(text,value){reg=/^([1-9](/d+)?|0)(/./d+)$/;errorInfo=text+" "+value+" 只能是正浮點(diǎn)數(shù),請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//非正浮點(diǎn)數(shù)(負(fù)浮點(diǎn)數(shù) + 0)function validate_float_notpositive(text,value){reg=/^/-([1-9](/d+)?|0)(/./d+)|0$/;errorInfo=text+" "+value+" 只能是是0或者負(fù)浮點(diǎn)數(shù),請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證負(fù)浮點(diǎn)數(shù)function validate_float_negative(text,value){reg=/^/-([1-9](/d+)?|0)(/./d+)$/;errorInfo=text+" "+value+" 只能是負(fù)浮點(diǎn)數(shù),請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證正浮點(diǎn)數(shù)多少位小數(shù)function validate_float_posbit(text,value,n){reg=eval("/^(//d+)(//.//d{"+n+"})$/");errorInfo=text+" "+value+" 只能是"+n+"位正浮點(diǎn)數(shù),請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證負(fù)浮點(diǎn)數(shù)多少位小數(shù)function validate_float_negbit(text,value,n){reg=eval("/^(-?//d+)(//.//d{"+n+"})$/");errorInfo=text+" "+value+" 只能是"+n+"位負(fù)浮點(diǎn)數(shù),請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//校驗(yàn)文本是否為空f(shuō)unction checknull(text,value){if(value=="" || typeof(value)=='undefined'){errorInfo=text+" "+value+" 不能為空,請(qǐng)重新輸入!";returnInfo(errorInfo);}}//驗(yàn)證由26個(gè)英文字母組成的字符串function validate_string_letter(text,value){reg=/^[A-Za-z]+$/;errorInfo=text+" "+value+" 只能是26位不區(qū)分大小寫(xiě)的英文字母組成,請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證由26個(gè)英文字母的大寫(xiě)組成的字符串function validate_string_bigletter(text,value){reg=/^[A-Z]+$/;errorInfo=text+" "+value+" 只能是26位大寫(xiě)的英文字母組成,請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證由26個(gè)英文字母的小寫(xiě)組成的字符串function validate_string_smallletter(text,value){reg=/^[a-z]+$/;errorInfo=text+" "+value+" 只能是26位小寫(xiě)的英文字母組成,請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證由數(shù)字和26個(gè)英文字母組成的字符串function validate_string_number(text,value){reg=/^[A-Za-z0-9]+$/;errorInfo=text+" "+value+" 只能是數(shù)字和26個(gè)英文字母組成,請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證由數(shù)字、26個(gè)英文字母或者下劃線組成的字符串function validate_string_numberandunderline(text,value){reg=/^/w+$/;errorInfo=text+" "+value+" 只能是數(shù)字、26個(gè)英文字母或者下劃線組成,請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證郵箱function validate_email(value){reg=/^[/w-]+(/.[/w-]+)*@[/w-]+(/.[/w-]+)+$/;errorInfo='郵箱 '+value+" 無(wú)效,請(qǐng)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證時(shí)如果選擇默認(rèn)調(diào)用此方法 金額類(lèi)型,可以是正整數(shù)或保留4位有效數(shù)字的正整數(shù)/**有問(wèn)題function validate_money_default(text,value){var reg=/^/d+(/./d{4})?$/;var errorInfo=text+" "+value+" 只能是正整數(shù)或保留4位有效數(shù)字,請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}**///驗(yàn)證輸入的只能是中文function validate_chinese(text,value){reg=/^[/u4e00-/u9fa5]+$/;errorInfo=text+" "+value+" 只能是中文字符,請(qǐng)重新輸入";verifyByReg(value,reg,errorInfo);}//驗(yàn)證輸入的只能是n位的中文function validate_bitchinese(text,value,bit){reg=eval("/^[//u4e00-//u9fa5]{"+bit+"}$/");alert(reg);errorInfo=text+" "+value+" 只能是"+bit+"位中文字符,請(qǐng)重新輸入";verifyByReg(value,reg,errorInfo);}//跟最大的值比較,此方法是看數(shù)字是否超出系統(tǒng)的上限function thanMax(text,number,max){if(number>max){errorInfo=text+" "+number+" 不能大于"+max+",請(qǐng)重新輸入";returnInfo(errorInfo);}}//跟最小的值比較,此方法是看數(shù)字是否低于系統(tǒng)的下限function thinMin(text,number,min){if(number<min){errorInfo=text +" "+number+" 不能小于"+min+",請(qǐng)重新輸入";returnInfo(errorInfo);}}//輸入文本的字符串是否在系統(tǒng)的指定長(zhǎng)度內(nèi)function isLimit(text,value,length){var arr=value.split("");if(arr.length>length){errorInfo=text +" "+value+" 字符過(guò)長(zhǎng),請(qǐng)輸入最多"+length+"位字符";returnInfo(errorInfo);}}//郵政編碼的驗(yàn)證function validate_zipcode(value){reg=/[1-9]/d{5}(?!/d)/;errorInfo='郵政編碼 '+value+" 有誤,請(qǐng)核實(shí)重新輸入";verifyByReg(value,reg,errorInfo);}//對(duì)數(shù)值類(lèi)型的數(shù)據(jù)進(jìn)行簡(jiǎn)單的驗(yàn)證和轉(zhuǎn)換,因?yàn)樵谠O(shè)置的時(shí)候沒(méi)有傳入這個(gè)參數(shù)的話就會(huì)出現(xiàn)js代碼錯(cuò)誤,所以應(yīng)該對(duì)數(shù)值型的數(shù)據(jù)進(jìn)行轉(zhuǎn)換//非空字符的判定function getZeroByNull(text,value){errorInfo=text+" "+value+" 不能為空,請(qǐng)核實(shí)重新輸入";if(value==null||value==''||value=='undefined'){returnInfo(errorInfo);}}//身份證驗(yàn)證,分為了兩種,15位和18位function validate_IdCard(value){var size=value.split("");if(size.length==15){reg=/^[1-9]/d{7}((0/d)|(1[0-2]))(([0|1|2]/d)|3[0-1])/d{3}$/;//15位身份證驗(yàn)證errorInfo='位身份證號(hào)碼 '+value+" 有誤,請(qǐng)核實(shí)重新輸入";}else if(size.length==18){reg=/^[1-9]/d{5}[1-9]/d{3}((0/d)|(1[0-2]))(([0|1|2]/d)|3[0-1])/d{3}([0-9]|X|x)$/;//18位身份證驗(yàn)證errorInfo='位身份證號(hào)碼 '+value+" 有誤,請(qǐng)核實(shí)重新輸入";}else{errorInfo="請(qǐng)輸入合法的身份證號(hào)碼!";returnInfo(errorInfo);}verifyByReg(value,reg,errorInfo);}//每個(gè)驗(yàn)證都需要調(diào)用此方法function verifyByReg(value,reg,errorInfo){if(value!=''&®!=''){var regExp = new RegExp(reg);if(!regExp.test(value)){alert(errorInfo);throw errorInfo;}}}//不進(jìn)過(guò)正則表達(dá)式驗(yàn)證,只需要返回信息,適合內(nèi)部調(diào)用此方法function returnInfo(message){alert(message);throw message;}//許可證號(hào) 生產(chǎn)企業(yè)的格式:QS 1234 2346 3456 其中空格可有可無(wú)///經(jīng)營(yíng)企業(yè)許可證的格式為: 湘010249 但是中國(guó)的省會(huì)簡(jiǎn)稱(chēng)可能會(huì)有三個(gè)字的,所以中文可以定義為1-3個(gè)function validate_license(value,type){if(type==1){reg=/^[a-zA-Z]{2}[ ]?(/d{4}[ ]?/d{4}[ ]?/d{4})$/;//生產(chǎn)企業(yè)許可證號(hào)errorInfo="生產(chǎn)許可證號(hào) "+value+" 有誤,請(qǐng)核實(shí)重新輸入!";}else if(type==2){reg=/^[/u4e00-/u9fa5]{1,3}/d{6}$/;errorInfo="經(jīng)營(yíng)許可證號(hào) "+value+" 有誤,請(qǐng)核實(shí)重新輸入!";}verifyByReg(value,reg,errorInfo);}//傳真 可以匹配的字符串如:+123 -999 999 ; +123-999 999 ;123 999 999 ;+123 999999等f(wàn)unction validate_fax(value){reg=/^([+]{0,1})?/d{1,3}[ ]?([-]?((/d)|[ ]){1,12})+$/;errorInfo="傳真 "+value+" 有誤,請(qǐng)重新輸入!";verifyByReg(value,reg,errorInfo);}//電話號(hào)碼可以輸入:電話或者手機(jī) 其中手機(jī)號(hào)碼可以是(+86)、(86)+號(hào)碼,也可以是號(hào)碼 電話的號(hào)碼可以是區(qū)號(hào)+號(hào)碼 區(qū)號(hào)-號(hào)碼 號(hào)碼function validate_tel(value){reg=/(^(0/d{2,3}([-|+])?)?/d{7}$)|(^(/d{3,4}([-|+])?)?/d{7,8}$)|(^([+]?(86)?[+]?)1[3-9]/d{9}$)/;errorInfo='電話號(hào)碼 '+value+" 有誤,請(qǐng)核實(shí)重新輸入!";verifyByReg(value,reg,errorInfo);}//日:大于0小于等于31的正整數(shù)function validate_day(value){reg=/(^[0]?[1-9]$)|(^[1-2]/d$)|(^[3][0-1]$)/;errorInfo='日期 '+value+" 有誤,請(qǐng)核實(shí)重新輸入!";verifyByReg(value,reg,errorInfo);}//月:合法月份function validate_month(value){reg=/(^[0]?[1-9]$)|(^[1][0-2]$)/;errorInfo='月份 '+value+" 有誤,請(qǐng)核實(shí)重新輸入!";verifyByReg(value,reg,errorInfo);}//年:合法年份 在這里合法的年份為公元1000年至公元2999年function validate_year(value){reg=/^[1-2]/d{3}$/;errorInfo='年份 '+value+" 有誤,請(qǐng)核實(shí)重新輸入!";verifyByReg(value,reg,errorInfo);}//百分比:合法百分比(0-100之前的數(shù)字)function validate_percentage(text,value){reg=/(^[1-9](/d)?(/./d+)?$)|(^0(/./d+)?$)|(^100$)/;errorInfo=text +" "+value+" 有誤,請(qǐng)輸入0到100的數(shù)字!";verifyByReg(value,reg,errorInfo);}//系數(shù):小于等于1的正數(shù) 在這里系數(shù)的小數(shù)點(diǎn)后面也是2位function validate_modulus(text,value){reg=/^[0](/./d{2})$|^[1](/.[0]{2})?$/;errorInfo=text+" "+value+" 有誤,請(qǐng)輸入小于0到1的數(shù)字!";verifyByReg(value,reg,errorInfo);}//驗(yàn)證非負(fù)的正數(shù),包含了正整數(shù)和正浮點(diǎn)數(shù)function validate_posnumhaszero(text,value){checknull(text,value);reg=/^[1-9](/d+)?(/./d+)?$|^0(/./d+)?$/;errorInfo=text+" "+value+" 有誤,請(qǐng)輸入大于或等于0 的數(shù)字!";verifyByReg(value,reg,errorInfo);}//驗(yàn)證非正的負(fù)數(shù),包含了負(fù)整數(shù)和負(fù)浮點(diǎn)數(shù)function validate_negnumhaszero(text,value){checknull(text,value);reg=/^[-][1-9](/d+)?(/./d+)?$|^[-]0(/./d+)?$/;errorInfo=text+" "+value+" 有誤,請(qǐng)輸入小于或等于0 的數(shù)字!";verifyByReg(value,reg,errorInfo);}//驗(yàn)證正數(shù),包含了正整數(shù)和正浮點(diǎn)數(shù)function validate_posnum(text,value){checknull(text,value);reg=/^[1-9](/d+)?(/./d+)?$|^0(/./d+)$/;errorInfo=text+" "+value+" 有誤,請(qǐng)輸入大于0 的數(shù)字!";verifyByReg(value,reg,errorInfo);}//驗(yàn)證負(fù)數(shù),包含了負(fù)整數(shù)和負(fù)浮點(diǎn)數(shù)function validate_negnum(text,value){checknull(text,value);reg=/^[-][1-9](/d+)?(/./d+)?$|^[-]0(/./d+)$/;errorInfo=text+" "+value+" 有誤,請(qǐng)輸入小于0 的數(shù)字!";verifyByReg(value,reg,errorInfo);}//驗(yàn)證數(shù),包括所有的數(shù)值,正數(shù)、負(fù)數(shù)、正浮點(diǎn)數(shù)、負(fù)浮點(diǎn)數(shù)function validate_allnum(text,value){checknull(text,value);reg=/^[-|+]?[1-9](/d+)?(/./d+)?$|^[-|+]?0(/./d+)?$/;errorInfo=text+" "+value+" 有誤,請(qǐng)輸入數(shù)字!";verifyByReg(value,reg,errorInfo);}希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答