标签:javascript js 正则表达式
之前写数据校验时忘记写dwz里的新增自定义校验,只要找到dwz.min.js文件的 if($.validator){ 的位置,在里面写上自己的校验正则表达式即可,我加了两个正则表达式,mac或mms 代码如下
if($.validator){ $.validator.addMethod("alphanumeric",function(value,element){ return this.optional(element)||/^\w+$/i.test(value);},"Letters, numbers or underscores only please"); $.validator.addMethod("lettersonly",function(value,element){ return this.optional(element)||/^[a-z]+$/i.test(value);},"Letters only please"); $.validator.addMethod("phone",function(value,element){ return this.optional(element)||/^[0-9 \(\)]{7,30}$/.test(value);},"Please specify a valid phone number"); $.validator.addMethod("mac",function(value,element){ return this.optional(element)||/^([a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2})|([a-fA-F0-9]{2}-[a-fA-F0-9]{2}-[a-fA-F0-9]{2}-[a-fA-F0-9]{2}-[a-fA-F0-9]{2}-[a-fA-F0-9]{2})$/.test(value);},"Please specify a valid phone number"); $.validator.addMethod("mms",function(value,element){ return this.optional(element)||/^mms$/.test(value);},"请mms");
然后只要在要用的html标签中加上class="mms"或class="mac" 即可
代码如下
<input type="text" name="aaa" class="required mac"/>
class第一个是非空,第二个是自己写的校验
标签:javascript js 正则表达式
原文地址:http://blog.csdn.net/u010935715/article/details/39177525