给jquery.Validation添加正则表达式验证方法
jQuery.Validation是个很好的表单验证插件,官网地址:https://jqueryvalidation.org/
如何添加正则表达式的验证规则?
$.validator.addMethod( "regex", function(value, element, regexp) { var re = new RegExp(regexp); return this.optional(element) || re.test(value); }, "无效输入." ); $("form.verify").find("input[name='firstname']").rules("add",{ regex: "^[a-zA-Z'.\\s]{2,40}$", required: true, minlength: 2, messages: { required: "名字不能为空", minlength: "长度至少是2位", regex: "名字仅由字母组成且长度不低于2位!" } });