码迷,mamicode.com
首页 > 其他好文 > 详细

validator检验(代码)

时间:2015-01-07 18:45:42      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

--------------异步判断数据是否存在----------------------------

$.validator.addMethod("isInfoExist", function(value, element) {  
        var id= $("#id").val();
        var isExist = false;
        $.ajax({
            url : WEB_ROOT + ‘/nihao/nihao.do‘,
            data : {
            ‘ren.name‘ : name
        },
        async : false,
        dataType : ‘json‘,
        success : function(response) {
            if (response)
                isExist = response.isExist; //后台传回一个布尔变量
        }
        });
        return isExist;
    }, "已存在!");

----------------------添加新的判断方法-----------------------------
    $.validator.addMethod("isExist", function(value, element) {
        var name = $("#name").val();
        var isExist = false;
        if(name==""){
            isExist=false;
        }else{isExist = true;};
        return isExist;
    }, "此处报错误信息!");

---------------validator中通过AJAX实现表单提交----------------------

    var options  = {
        url:$("#form").attr(‘action‘),
        type:‘post‘,
        dataType : ‘json‘,
        success:function(data){
        $(document.body).popMsg({
            msg : "添加成功!",                    //弹出提示信息
            confirm : function() {
            window.location.href = WEB_ROOT + "/nihao/nihao.jsp";  //跳转jsp页面
        },
         close : function() {
          window.location.href = WEB_ROOT + "nihao.jsp";
        }
        });
    },
    error:function(data){
        $(document.body).popMsg({
            msg : "添加失败!",
            confirm : function() {

        }
        });
    }
    };

var validator = $("#form").validate({   //此处是绑定form表单 id

------------------------实现表单ajax提交--------------------------------------------
        submitHandler : function(form){
        $(form).ajaxSubmit(options);
    },

------------------------------------------------------------------------------------


    ‘rules‘ : {                                   // 定义验证规则


          ‘ren.id‘ : {     // 控件jsp中 实体name
            required : true,                //validator有很多自带属性 required 表示非空
           isInfoExist:true      //自定义方法
        },
       ‘ren.name‘ : {
            required : true,
           isExist:true         
        },
    },

--------------------判断提示信息------------------------------
    messages : {


          ‘ren.id‘ : {                                      // 控件jsp中 实体name
            required : "请填写id",
        },
       ‘ren.name‘ : {
            required : "请填写name",
        },  
    },

----------------------提示信息位子------------------------------
    errorPlacement : function(error, element) {
        if (element.is(":radio"))
            error.appendTo(element.parent());
        else if (element.is(":checkbox"))
            error.appendTo(element.parent());
        else if (element.is("input[name=captcha]"))
            error.appendTo(element.parent());
        else
            error.insertAfter(element);
    },

----------------------成功之后清空提示信息追加right样式(可以添加图片)------------------------------
    success : function(label) {
        label.html(" ").addClass("right");
    },
    error : function() {
    }
    });

validator检验(代码)

标签:

原文地址:http://www.cnblogs.com/songwy/p/4208931.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!