【问题标题】:jquery validation with ajax call使用 ajax 调用的 jquery 验证
【发布时间】:2018-12-01 02:57:24
【问题描述】:

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

我用

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

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

谁能帮我实现这个。

【问题讨论】:

    标签: jquery jquery-validate


    【解决方案1】:
    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;
    

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

    【讨论】:

    • 感谢达人。我会试驾一下,我想就是这样
    • 我怎样才能只为远程添加消息......或者我怎样才能'做某事'onsuccess
    • 您刚刚为我节省了数小时的时间。谢谢
    【解决方案2】:

    嗯,这对我有用...

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

    【讨论】:

      【解决方案3】:

      您的服务器语言是什么? 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();
            }
          }
      });
      

      【讨论】:

      • 他正在使用 validate,它内置了 ajax。
      • 认为作者的意思是使用 jQuery 验证插件:docs.jquery.com/Plugins/Validation。不仅仅是一个 ajax 调用。
      【解决方案4】:

      首先我想告诉你,如果你在 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';
       }`
      

      试试这个。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-29
        • 1970-01-01
        • 2021-02-09
        • 2012-03-30
        相关资源
        最近更新 更多