jquery.validate验证表单,自己的js提交数据,返回json

$.metadata.setType("attr", "vld");

$.validator.addMethod("username", function(value) {

var p = /^[0-9a-zA-Z\u4e00-\u9fa5\.\-@_]+$/;

return p.exec(value) ? true : false;

}, "Please enter only letters,digits,chinese and '_','-','@'");



$.metadata.setType("attr", "vld");

$.validator.addMethod("username", function(value) {

var p = /^[0-9a-zA-Z\u4e00-\u9fa5\.\-@_]+$/;

return p.exec(value) ? true : false;

}, "只能输入字母、数字、中文和'_','-','@'符号");


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

var length = value.length;

var mobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;

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


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



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

var length = value.length;

var tel = /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;

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


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


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

var length = value.length;

var mobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;

var tel = /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;

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


}, "请正确填写您的联系电话");


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

var length = value.length;

//***正则表达式(15位)

var isIDCard1=/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{2}(\d|X|x)$/;

//***正则表达式(18位)

var isIDCard2=/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|X|x)$/;

return this.optional(element) || (isIDCard1.test(value) || isIDCard2.test(value));


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


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

var length = value.length;

var zipCode = /^[1-9]{1}(\d+){5}$/;

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


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


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

var mail = /^[a-z0-9._%-]+@([a-z0-9-]+\.)+[a-z]{2,4}$/;

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

}, "邮箱格式不对");










  <form class="form b-mar-b-20" id="VipApply_AddForm" action="/addVipApply.jspx">

    <span class="form-item-name">用户名<span class="c-red">*</span></span>

    <input  type="text" class="input-text" id="loginName" name="loginName" vld="            {required:true,rangelength:[3,20],username:true,messages:{required:'请输入用户            名',rangelength:'用户名由3到14位的中文字符、英文字母、数字组成'}}" />

  </form>

$("#VipApply_AddForm").validate({

    submitHandler: function(form) {  //通过之后回调

       $.post("${base}/addVipApply.jspx",

            $('#VipApply_AddForm').serialize(),

            function(data){

if(data.status==0){

                     alert("VIP申请已提交,请耐心等待审核结果!");

                 location.href='${base}/hymsa/';

}else if(data.status==1) {

                     alert("验证码错误");

                     $("#vipApplyCaptcha").click();

                }

        },'json');

},

invalidHandler: function(form, validator) {  //不通过回调

    return false;

}

    });

});



    @RequestMapping(value = "**/addVipApply.jspx")

    public void addVipApply(VipApplyEntity entity,String captcha, HttpServletRequest             request,HttpServletResponse response, ModelMap model) throws JSONException {

            JSONObject json = new JSONObject();

try {

this.vipApplyService.save(entity);

json.put("status", 0);

} catch (Exception e) {

json.put("status", 1);

e.printStackTrace();

ResponseUtils.renderJson(response, json.toString());

}


你可能感兴趣的:(返回JSON,自己的js提交数据)