用ajax调用进行jquery验证 [英] jquery validation with ajax call

查看:75
本文介绍了用ajax调用进行jquery验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery验证,我需要验证电子邮件.

I am using the jquery validation and i need to validate an email.

我用

    $("#myForm").validate({
        rules: 
            email: {
                required: true,
                email: true
            }
})

太好了.问题是我需要进行ajax调用以验证给定的电子邮件是否已经存在.如果存在,则显示消息此电子邮件已存在.请选择其他".

SO FAR so good. The problem is that i need to make an ajax call to verify if given email already exist.If exist display message "This email already exits.Please selecte other".

任何人都可以帮助我实现这一目标.

Can anyone help me implement this.

推荐答案

remote: "/some/remote/path"

该路径将在$ _GET中传递该字段的值. 所以..在您的情况下实际被称为:

That path will be passed the value of the field in a $_GET. so.. what actually gets called in your case would be:

/some/remote/path?email=someemailuriencoded

让服务器端代码只返回true或false文本.

Have the server side code return just the text true or false.

然后相应的消息也称为远程.

Then the corresponding message also named remote.

remote: "The corresponding email already exists"

我的类似代码:

$("#password_reset").validate({
  rules: { 
    email: { required: true, email: true, minlength: 6, remote: "/ajax/password/check_email" }
  }, 
  messages: { 
    email: { required: "Please enter a valid email address", minlength: "Please enter a valid email address", email: "Please enter a valid email address", remote: "This email is not registered" }
  }, 
  onkeyup: false,
  onblur: true
});

php中相应的服务器端代码:

The corresponding server side code in php:

$email_exists = $db->prows('SELECT user_id FROM users WHERE email = ? LIMIT 1', 's' , $_GET['email'] );
if ( $email_exists ) { echo 'true'; } else { echo 'false'; }
exit;

当然,这是使用我的数据库抽象的东西,但是您明白了.

Of course that's using my database abstraction stuff, but you get it.

这篇关于用ajax调用进行jquery验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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