jQuery验证插件:使用自定义Ajax方法 [英] JQuery Validation Plugin: Use Custom Ajax Method

查看:100
本文介绍了jQuery验证插件:使用自定义Ajax方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在可能的情况下通过 jQuery表单验证插件寻求帮助.

Looking for some assistance with the Jquery form validation plugin if possible.

我通过对数据库进行Ajax调用来模糊地验证表单的电子邮件字段,该数据库将检查电子邮件字段中的文本当前是否在数据库中.

I am validating the email field of my form on blur by making an ajax call to my database, which checks if the text in the email field is currently in the database.

    // Check email validity on Blur
       $('#sEmail').blur(function(){
       // Grab Email From Form
       var itemValue = $('#sEmail').val();
        // Serialise data for ajax processing
        var emailData = {
            sEmail: itemValue
        }
        // Do Ajax Call
         $.getJSON('http://localhost:8501/ems/trunk/www/cfcs/admin_user_service.cfc?method=getAdminUserEmail&returnFormat=json&queryformat=column', emailData, function(data){

                    if (data != false) {
                        var errorMessage = 'This email address has already been  registered';
                    }
                    else {
                        var errorMessage = 'Good'
                    }
                })
    });

我想做的是将此调用合并到我的JQuery Validation插件的规则中.例如

What I would like to do, is encorporate this call into the rules of my JQuery Validation Plugin...e.g

    $("#setAdminUser").validate({
      rules:{
             sEmail: {
                  required: function(){
                  // Replicate my on blur ajax email call here
                 }                  
             },
            messages:{
                sEmail: {
                    required: "this email already exists"       

            }
        });

想知道是否有实现此目标的方法吗?非常感谢

Wondering if there is anyway of achieving this? Many thanks

推荐答案

您正在执行AJAX请求,因此:自定义验证器返回true或false时,验证已经完成.

You are doing an AJAX request, ergo: the validation is already finished working when your custom validator returns either true or false.

您将需要使用异步.另请参阅此帖子:如何让jQuery执行同步而不是异步的Ajax请求?

You will need to work with async. See also this post: How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

让我们说我们有一个自定义验证来检查唯一移动设备,因此我们将添加如下所示的新方法来检查不正常的移动电话号码:

Lets say we have a custom validation to check the Unique Mobile, So we will add a new method as follows to check the unqiue Mobile no :

jQuery.validator.addMethod("uniqueMobile", function(value, element) {
    var response;
    $.ajax({
        type: "POST",
        url: <YOUR SERVER SCRIPT/METHOD>,
        data:"mobile="+value,
        async:false,
        success:function(data){
            response = data;
        }
    });
    if(response == 0)
    {
        return true;
    }
    else if(response == 1)
    {
        return false;
    }
    else if(response == 'logout')
    {
        return false;
    }
}, "Mobile is Already Taken");

这篇关于jQuery验证插件:使用自定义Ajax方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆