使用 ajax 调用进行 jquery 验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1564711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:44:06  来源:igfitidea点击:

jquery validation with ajax call

jqueryjquery-validate

提问by Daren Schwenke

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

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

I use

我用

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

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".

到目前为止,一切都很好。问题是我需要进行 ajax 调用以验证给定的电子邮件是否已经存在。如果存在则显示消息“此电子邮件已经存在。请选择其他”。

Can anyone help me implement this.

谁能帮我实现这个。

回答by Daren Schwenke

remote: "/some/remote/path"

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

该路径将在 $_GET 中传递该字段的值。所以..在你的情况下实际被调用的是:

/some/remote/path?email=someemailuriencoded

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

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

Then the corresponding message also named remote.

然后相应的消息也命名为remote。

remote: "The corresponding email already exists"

My code for something similar:

我的类似代码:

$("#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
});

The corresponding server side code in php:

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.

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

回答by Rbacarin

Well, this works for me...

嗯,这对我有用......

 $('[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;
         }
  });

note that I have a input with the id 'txtMail'

请注意,我有一个 ID 为“txtMail”的输入

回答by Madhuri Patel

first of all i want to tell you that if you are using post method in ajax then dont pass email in url.

首先我想告诉你,如果你在 ajax 中使用 post 方法,那么不要在 url 中传递电子邮件。

 $.ajax({
        type: "POST",
        url: URLROOT + '/register/check/';
        data: {email:email};
         success: function (result) {
            if (result == 'exist') {
              return false;
             } else {
             return true;
             }
          }
      });`

after that you remove parameter from controller's function.

之后,您从控制器的功能中删除参数。

`public function check(l) {
  if($this->userModel->findUserByEmail($_POST['email'])==true)) {
   return 'exist';
  }
  return 'false';
 }`

Try with this.

试试这个。

回答by k0ni

Whats your server language? PHP or ASP?

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

This is the jQuery part:

这是 jQuery 部分:

$.ajax({
    type: "POST",
    url: "YourWebserviceOrWhatEver",
    data: "{'Email':'[email protected]'}",
    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();
      }
    }
});