开发者

jQuery validator add method: custom messages

开发者 https://www.devze.com 2023-03-18 21:07 出处:网络
I am using following code to show custom messages inside add method of jquery validation plugin. I referred this link: http://www.mainframes.co.uk/index.php/2011/04/07/jquery-form-validator-custom-val

I am using following code to show custom messages inside add method of jquery validation plugin. I referred this link: http://www.mainframes.co.uk/index.php/2011/04/07/jquery-form-validator-custom-validation-method-using-addmethod-for-validating-date-of-birth-dob/

$.validator.addMethod("nameId",function(value,element){

            var result = true;
        //check if pattern matches
                var pattern = new RegExp(<pattern>);                
            if(pattern.test(value)){            
                    //get availability via ajax call
                    $.ajaxSetup({
                        async: false,   
                        "error":function() {   
                            alert("error");
                    }});
                    $.getJSON("<url>", 
                    {
                        param: value
                    },
                    function(data) {

                            if(condition){
                                result = true;
                            }else{
                                $.validator.messages.nameId = "Msg 1";
                                    resul开发者_JS百科t = false;                     
                            }
                    });

            }else{
                                $.validator.messages.nameId = "Msg 2";
                                result = false;                         
            }
    return result;
 },"");

This doesn't show any errors while validating. What could be wrong above...


What happens if you put the .getJSON call inside of the success callback function of the .ajaxSetup? Like this:

$.validator.addMethod("nameId",function(value,element){

    var result = true;

    var pattern = new RegExp(<pattern>);

    if(pattern.test(value)){            
        //get availability via ajax call
        $.ajaxSetup({
            async: false,
            "success": function() {
                $.getJSON("<url>", 
                {
                    param: value
                },
                function(data) {
                    if(condition){
                        result = true;
                    }else{
                        $.validator.messages.nameId = "Msg 1";
                            result = false;                     
                    }
                });
            },
            "error":function() {   
                alert("error");
            }
        });

    }else{
        $.validator.messages.nameId = "Msg 2";
        result = false;                         
    }
    return result;
 },"");


Add this just before your return result line:

$.validator.messages.myvalidator = customError;


You cant have asynchronous callbacks inside a validation method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号