使用ajax调用进行jqueryvalidation

我正在使用jqueryvalidation,我需要validation一封电子邮件。

我用

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

到现在为止还挺好。 问题是我需要进行ajax调用来validation给定的电子邮件是否已经存在。如果存在显示消息“此电子邮件已经退出。请选择其他”。

任何人都可以帮我实现这一点。

 remote: "/some/remote/path" 

该路径将传递$ _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; 

当然,这是使用我的数据库抽象的东西,但你得到它。

嗯,这对我有用……

  $('[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’的输入

你的服务器语言是什么? PHP还是ASP?

这是jQuery的一部分:

 $.ajax({ type: "POST", url: "YourWebserviceOrWhatEver", data: "{'Email':'your@ema.il'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { if(msg.EmailExists){ //Email exists alert("This email already exists. Please select other"); } else { //Email doesn't exist yet doSomething(); } } });