JQuery validate验证 自定义

http://www.w3cschool.cc/jquery/jquery-plugin-validate.html

http://blog.163.com/zhao_jinggui/blog/static/169620429201172942347465/

 

$().ready(function() {

 jQuery.validator.addMethod("isMobile", function(value, element) {
  var length = value.length;
  var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
  return this.optional(element) || (length == 11 && mobile.test(value));
 }, "请正确填写您的手机号码");

 jQuery.validator.addMethod("isphone", function(value, element) {
  var length = value.length;
  var phone = /(^(\d{3,4}-)?\d{6,8}$)|(^(\d{3,4}-)?\d{6,8}(-\d{1,5})?$)|(\d{11})/;
  return this.optional(element) || (phone.test(value));
 }, "请填写正确的电话号码");
 
 jQuery.validator.addMethod("ischinese", function(value, element) {
  var chinese = /^[\一-\龥]+$/i;
  return this.optional(element) || (chinese.test(value));
 }, "只能输入汉字");
 
 jQuery.validator.addMethod("ismypassword", function(value, element,param) {
//  var specialstr = /[!,.@#$%^&*?_~]/;
//  var numberstr= /^(\d+[a-zA-Z]\w*)|([a-zA-Z]+\d\w*)$/;
//  return this.optional(element) && (numberstr.test(value)) && (specialstr.test(value));
          var regu1 =/[A-Za-z]/;
    var regu2=/\d/;
    var regu3=/[!,.@#$%^&*?_~]/;
             var b1=regu1.test(value);
             var b2=regu2.test(value);
             var b3=regu3.test(value);
             var b=b1&&b2&&b3;
    return b == eval(param);
 }, "密码必须输入字母和数字");

 $("#myform").validate( {

  rules : {
   userName : {
    required : true,
    minlength : 6,
    maxlength : 200
   },
   userEmail : {
    required : true,
    email : true
   },
   userPwd : {
    required : true,
    minlength : 8,
    ismypassword : true
   },
   confuserPwd : {
    required : true,
    minlength : 8,
    equalTo: "#userPwd"
   },
   userRname : {
    required : true,
    minlength : 6,
    maxlength : 200,
       ischinese:true
   },
   userMphone : {
    required : true,
    isMobile : true
   },
   userPhone : {
    required : true,
    isphone : true
   }
  },

  messages : {
   userName : {
    required : "请输入用户名称",
    minlength : "字符长度不能小于6个字符",
    maxlength : "字符长度不能大于200个字符"
   },
   userEmail : {
    required : "请输入邮箱地址",
    email : "请输入正确的邮箱地址"
   },
   userPwd : {
    required : "请输入密码",
    minlength : "字符长度不能小于8个字符",
    ismypassword : "密码必须由数字、英文字母和特殊字符(!,.@#$%^&*?_~)组成"
   },
   confuserPwd : {
    required : "请输入确认密码",
    minlength : "字符长度不能小于8个字符",
    equalTo :"两次密码输入不一致"
   },
   userRname : {
    required : "请输入真实姓名",
    minlength : "字符长度不能小于6个字符",
    maxlength : "字符长度不能大于200个字符",
       ischinese:  "只能输入汉字"
   },
   userMphone : {
    required : "请输入手机号码",
    isMobile : "请输入正确的手机号码"
   },
   userPhone : {
    required : "请输入电话号码",
    isphone : "请输入正确的电话号码"
   }
  },
   errorElement : "em",
   success : function(em) { 
   em.text(" ") .addClass("success");
    }
 });
 
});

 

 /*表单验证样式*/
em {
 font-style:normal; /*字体不倾斜*/
 font-weight: 500;
 padding-right: 1em;
 vertical-align: middle;
 color: red;
}
em.error {
 background: url("../images/unchecked.gif") no-repeat 0px 0px;
 padding-left: 16px;
}
em.success {
 background: url("../images/standard_msg_ok.gif") no-repeat 0px 0px;
 padding-left: 16px;
}

  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。