首页 > 代码库 > jquery+ajax+form表单验证

jquery+ajax+form表单验证

$().ready(function() {
  $("#publishForm").validate({
  onsubmit:true,// 是否在提交是验证
  onfocusout:false,// 是否在获取焦点时验证
  onkeyup :false,// 是否在敲击键盘时验证

rules: {
  username: {
    required: true,
    maxlength: 100
  },
  conbox: {
    required: true,
    maxlength: 140
  }
},
messages:{
  username: {
    required: "请填写昵称",
    maxlength: "姓名不超过100个字符"
    },
  conbox: {
    required: "请填写发表内容",
    maxlength: "发表内容不超过140个字符"
  }
},
submitHandler: function(form) { //通过之后回调

$.ajax({
  url : "{:U(‘index/add‘)}",
  type : "post",
  dataType : "json",
  data: {
    username: $("#username").val(),
    user_content: $("#conbox").val()
  },
  success : function(msg) {
    //要执行的代码
  }
});
},
invalidHandler: function(form, validator) {return false;}
});
});

jquery+ajax+form表单验证