jQuery验证 - 使用AJAX调用验证电子邮件 [英] jQuery Validation - validate email using AJAX call

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

问题描述

我有一张邀请表,只接受GMail处理的电子邮件。我正在尝试插入异步验证,但不确定应该从服务器返回什么以及要使用哪些Javascript处理程序。

I have an invitation form that should only accept emails handled by GMail. I'm trying to plug in asynchronous validation, but not sure what should be returned from the server and what Javascript handlers to use.


  1. I如果有人输入雅虎电子邮件,我想在电子邮件输入字段旁边显示不是GMail地址

  2. 我希望从后端收到Not a GMail address消息。

我的尝试(不太好):

$('#invite').validate({
    submitHandler: function(form, e) {
        $.ajax({
            type: 'POST',
            url: $(form).attr('action'),
            data: {
                email: $(form).find('#email').val()
            },
            success: function() {debugger;},
            error: function() {alert('failed');}
        });

        return false;
    },
    invalidHandler: function(event, validator) {
        debugger;
    },
    rules: {
        email: {
          required: true,
          email: true,
          remote: {
            url: "/invite.php",
            type: "post",
            async: true,
            data: submittedEmail
        }
      }
    }
});

谢谢!

推荐答案

问题解决了。

Javascript代码:

Javascript code:

$('#invite').validate({
    rules: {
        email: {
            required: true,
            email: true,
            remote: {
                url: '/admin/isgmail/',
                type: 'POST',
                dataType: 'json',
                data: {
                    email: function() {
                        return $('#email').val();
                    }
                }
            }
        }
    },
    messages: {
        email: "GMail email is required."
    },
    onkeyup: false
});

invite.php 应返回字符串true如果验证成功,字符串false如果有错误或JSON编码的字符串描述错误: echo json_encode('这是不是Google帐户');

invite.php should return string "true" if validation is successful, string "false" if there was an error OR JSON-encoded string describing the error: echo json_encode('This is not a Google account');.

这篇关于jQuery验证 - 使用AJAX调用验证电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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