使用ajax调用进行jquery验证
我正在使用jquery验证,我需要验证一封电子邮件 我用 到目前为止还不错。问题是,我需要进行一个ajax调用,以验证给定的电子邮件是否已经存在。如果存在,则显示消息“此电子邮件已经存在。请选择其他” 谁能帮我实现这个使用ajax调用进行jquery验证,jquery,jquery-validate,Jquery,Jquery Validate,我正在使用jquery验证,我需要验证一封电子邮件 我用 到目前为止还不错。问题是,我需要进行一个ajax调用,以验证给定的电子邮件是否已经存在。如果存在,则显示消息“此电子邮件已经存在。请选择其他” 谁能帮我实现这个 remote: "/some/remote/path" 该路径将通过$\u GET传递字段的值。 所以在您的案例中,实际调用的是: /some/remote/path?email=someemailuriencoded 让服务器端代码只返回文本true或false 然后相应的
remote: "/some/remote/path"
该路径将通过$\u GET传递字段的值。
所以在您的案例中,实际调用的是:
/some/remote/path?email=someemailuriencoded
让服务器端代码只返回文本true或false
然后相应的消息也被命名为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中相应的服务器端代码:
$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;
当然,这是使用我的数据库抽象的东西,但你明白了。你的服务器语言是什么?PHP还是ASP 这是jQuery部分:
$.ajax({
类型:“POST”,
url:“YourWebserviceOrWhatEver”,
数据:“{'Email':'your@ema.il'}",
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(msg){
如果(msg.EmailExists){
//电子邮件存在
警报(“此电子邮件已存在。请选择其他”);
}
否则{
//电子邮件还不存在
doSomething();
}
}
});
好吧,这对我很有用
$('[id$=txtEmail]').rules("add", { required: true, email: true,
remote:function(){
var checkit={
type: "POST",
url: WebServicePathComplete+"VerifyEmail",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{'email':'" +$('[id$=txtEmail]').val() + "'}"
};
return checkit;
}
});
请注意,我有一个id为“txtMail”的输入。首先,我想告诉您,如果您在ajax中使用post方法,那么不要在url中传递电子邮件
$.ajax({
type: "POST",
url: URLROOT + '/register/check/';
data: {email:email};
success: function (result) {
if (result == 'exist') {
return false;
} else {
return true;
}
}
});`
然后从控制器的函数中删除参数
`public function check(l) {
if($this->userModel->findUserByEmail($_POST['email'])==true)) {
return 'exist';
}
return 'false';
}`
试试这个。谢谢Daren。我会试驾它,我想这就是我怎样才能只为远程添加消息…或者我怎样才能在成功后“做点什么”你刚才为我节省了几个小时的时间。感谢她使用了内置ajax的validate。我想作者的意思是使用jQuery验证插件:。不仅仅是ajax调用。
`public function check(l) {
if($this->userModel->findUserByEmail($_POST['email'])==true)) {
return 'exist';
}
return 'false';
}`