简体   繁体   English

jQuery .Validate()远程成功函数和消息

[英]jQuery .Validate() Remote sucess function and message

I need to validate an email field using the jQuery.Validate() plugin it has a "Remote" method which we have to post to a server and server needs to return true or false, Instead of using the traditional way(adding a function on the server to return true or false), I need to get json response from the server and on success run a function to decide where to return true or false... 我需要使用jQuery.Validate()插件来验证电子邮件字段,该插件具有“远程”方法,我们必须将其发布到服务器,并且服务器需要返回true或false,而不是使用传统方式(在函数上添加函数服务器返回true或false),我需要从服务器获取json响应,并成功运行一个函数来确定在哪里返回true或false ...

Here is what the response from the server looks like (I'm using the Yii ajax form validation) 这是服务器响应的样子(我正在使用Yii ajax表单验证)

{
   "zipcode":[
      "Zipcode cannot be blank."
   ],
   "email":[
      "Email is already registered"
   ],

}

If email is listed on the array that means the validation had errors So I create a Remote validation rule like follows: 如果在阵列上列出了电子邮件,则表明验证有误,因此我创建了如下的远程验证规则:

    'email': {
        required: true,
        email: true,
        remote:{
            type:"POST",
            url:url,
            dataType:'json',
            data:{'email':function(){
                $('#email').val();
            },ajax:'validate'},
            success:function(resp){

                $.each(resp,function(index,value){
                    if(index == "email")
                        return false;
                });

            }
        }
    },

But does not work, also I did not found anywhere where I can add the error message for remote validation, I would like to pass the array email value as the message... 但是不起作用,我也找不到可以添加错误消息进行远程验证的任何地方,我想将数组电子邮件值作为消息传递...

You need to replace your success function with below function : 您需要用以下功能替换成功功能:

dataFilter: function (data) {
                return 'false'; //Email not exist
                return 'true'; //Email already exist
            }

Please pass true/false value as a string. 请以字符串形式传递true / false值。

For Display Message : 对于显示消息:

remote:{......},
messages:{email:'Please enter valid email', remote : "{0} is already exist"}

I think this will be help. 我认为这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM