天天看点

jquery validate form

$(document).ready(function(){

$.validator.setDefaults({

  submitHandler: function(form) { form.submit(); }

});

// 中文字两个字节

jQuery.validator.addMethod("byteRangeLength", function(value, element, param) {

  var length = value.length;

  for(var i = 0; i < value.length; i++){

   if(value.charCodeAt(i) > 127){

    length++;

   }

  }

  return this.optional(element) || ( length >= param[0] && length <= param[1] );

}, "请确保输入的值在3-15个字节之间(一个中文字算2个字节)");

// 身份证号码验证

jQuery.validator.addMethod("isIdCardNo", function(value, element) {

  return this.optional(element) || isIdCardNo(value);

}, "请正确输入您的身份证号码");

// 字符验证

jQuery.validator.addMethod("userName", function(value, element) {

  return this.optional(element) || /^[/u0391-/uFFE5/w]+$/.test(value);

}, "用户名只能包括中文字、英文字母、数字和下划线");

// 手机号码验证

jQuery.validator.addMethod("isMobile", function(value, element) {

  var length = value.length;

  return this.optional(element) || (length == 11 && /^(((13[0-9]{1})|(15[0-9]{1}))+/d{8})$/.test(value));

}, "请正确填写您的手机号码");

// 电话号码验证

jQuery.validator.addMethod("isPhone", function(value, element) {

  var tel = /^(/d{3,4}-?)?/d{7,9}$/g;

  return this.optional(element) || (tel.test(value));

}, "请正确填写您的电话号码");

// 邮政编码验证

jQuery.validator.addMethod("isZipCode", function(value, element) {

  var tel = /^[0-9]{6}$/;

  return this.optional(element) || (tel.test(value));

}, "请正确填写您的邮政编码");

$(regFrom).validate({

  rules: {

   userName: {

    required: true,

    userName: true,

    byteRangeLength: [3,15]

   },

   password: {

    required: true,

    minLength: 5

   },

   repassword: {

    required: true,

    minLength: 5,

    equalTo: "#password"

   },

   question: {

    required: true

   },

   answer: {

    required: true

   },

   realName: {

    required: true

   },

   cardNumber: {

    isIdCardNo: true

   },

   mobilePhone: {

    isMobile: true

   },

   phone: {

    isPhone: true

   },

   email: {

    required: true,

    email: true

   },

   zipCode: {

    isZipCode:true

   }

  },

  messages: {

   userName: {

    required: "请填写用户名",

    byteRangeLength: "用户名必须在3-15个字符之间(一个中文字算2个字符)"

   },

   password: {

    required: "请填写密码",

    minlength: jQuery.format("输入{0}.")

   },

   repassword: {

    required: "请填写确认密码",

    equalTo: "两次密码输入不相同"

   },

   question: {

    required: "请填写您的密码提示问题"

   },

   answer: {

    required: "请填写您的密码提示答案"

   },

   realName: {

    required: "请填写您的真实姓名"

   },

   email: {

    required: "请输入一个Email地址",

    email: "请输入一个有效的Email地址"

   }

  },

  errorPlacement: function(error, element) {

   error.appendTo( element.parent() );

  },

  success: function(label) {

   // set   as text for IE

   label.html(" ").addClass("checked");

  },

  focusInvalid: false,

  onkeyup: false

});

// 输入框获得焦点时,样式设置

$('input').focus(function(){

  if($(this).is(":text") || $(this).is(":password"))

   $(this).addClass('focus');

  if ($(this).hasClass('have_tooltip')) {

   $(this).parent().parent().removeClass('field_normal').addClass('field_focus');

  }

});

// 输入框失去焦点时,样式设置

$('input').blur(function() {

  $(this).removeClass('focus');

  if ($(this).hasClass('have_tooltip')) {

   $(this).parent().parent().removeClass('field_focus').addClass('field_normal');

  }

});

});

  1. <form class="cmxform" id="signupForm " method="get" action="">
  2. <p>
  3. <label for="username">用户名</label>
  4. <input id="username" name="username " />
  5. </p>
  6. <p>
  7. <label for="password">密码</label>
  8. <input id="password" name="password " type="password" />
  9. </p>
  10. <p>
  11. <label for="confirm_password">确认密码</label>
  12. <input id="confirm_password" name="confirm_password " type="password" />
  13. </p>
  14. <p>
  15. <label for="email">邮箱</label>
  16. <input id="email" name="email " />
  17. </p>
  18. <p>
  19. <label for="agree">是否同意</label>
  20. <input type="checkbox" id="agree" name="agree " />
  21. </p>
  22. <p>
  23. <input class="submit" type="submit" value="Submit"/>
  24. </p>
  25. </form>

javascript部分

  1. $().ready(function() {
  2. $("#signupForm ").validate({
  3. rules: {
  4. username: {
  5. required: true,
  6. minlength: 2
  7. },
  8. password: {
  9. required: true,
  10. minlength: 5
  11. },
  12. confirm_password: {
  13. required: true,
  14. minlength: 5,
  15. equalTo: "#password"
  16. },
  17. email: {
  18. required: true,
  19. email: true
  20. }
  21. },
  22. messages: {
  23. username: {
  24. required: "请输入用户名",
  25. minlength: "用户名不能少于2个字符"
  26. },
  27. password: {
  28. required: "请输入密码",
  29. minlength: "密码不能少于5个字符"
  30. },
  31. confirm_password: {
  32. required: "请输入确认密码",
  33. minlength: "确认密码不能少于5个字符",
  34. equalTo: "密码输入不一致"
  35. },
  36. email: "请输入有效邮箱"
  37. }
  38. });
  39. });