【问题标题】:jQuery remote validate against email verification servicejQuery远程验证电子邮件验证服务
【发布时间】:2018-05-16 04:44:24
【问题描述】:

我需要根据电子邮件验证服务验证电子邮件。

问题是我无法控制服务 php。 服务返回“有效”和“无效”响应。

据我了解,jQuery 验证要求是真还是假。

远程网址是http://i2srv.com/validate/

示例:http://i2srv.com/validate/?email=skyhawk133@gmail.com 必须以这种格式发送。

这是我尝试过的。

remote: {      
    url: "http://i2srv.com/validate/",
    type: "post",
    complete: function(data) {
        if( data.responseText == "valid" ) {
            alert("Email is OK");
        }
    }
}

【问题讨论】:

    标签: jquery validation email


    【解决方案1】:

    根据remote 方法的文档选项,深度扩展默认值(dataType:"json", data:{nameOfTheElement:valueOfTheElement})。您提供的任何选项都将覆盖默认值。

    也是最重要的

    服务器端资源通过 jQuery.ajax (XMLHttpRequest) 和 获取与已验证名称对应的键/值对 元素及其作为 GET 参数的值。

    所以你不必使用post 方法,只需将dataType 更改为html

    remote: {
          url: "https://i2srv.com/validate/",
          dataType: 'html',
          complete: function(data) {
            if (data.responseText == "valid") {
              alert("Email is OK");
            }
          }
        }
    

    请看下面的演示

    $(document).ready(function() {
      $("#signupform").validate({
        rules: {
          firstname: "required",
          lastname: "required",
          email: {
            required: true,
            email: true,
            remote: {
              url: "https://i2srv.com/validate/",
              dataType: 'html',
              complete: function(data) {
                if (data.responseText == "valid") {
                  alert("Email is OK");
                }
              }
            }
          }
        },
        messages: {
          firstname: "please enter firstname",
          lastname: "please enter firstname",
        }
      });
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.js"></script>
    <div id="signupwrap">
      <form id="signupform" autocomplete="off" method="get" action="">
        <table>
          <tr>
            <td class="label">
              <label id="lfirstname" for="firstname">First Name</label>
            </td>
            <td class="field">
              <input id="firstname" name="firstname" type="text" value="" maxlength="100">
            </td>
            <td class="status"></td>
          </tr>
          <tr>
            <td class="label">
              <label id="llastname" for="lastname">Last Name</label>
            </td>
            <td class="field">
              <input id="lastname" name="lastname" type="text" value="" maxlength="100">
            </td>
            <td class="status"></td>
          </tr>
    
          <tr>
            <td class="label">
              <label id="lemail" for="email">Email Address</label>
            </td>
            <td class="field">
              <input id="email" name="email" type="text" value="" maxlength="150">
            </td>
            <td class="status"></td>
          </tr>
    
          <tr>
            <td class="label">
              <label id="lsignupsubmit" for="signupsubmit">Signup</label>
            </td>
            <td class="field" colspan="2">
              <input id="signupsubmit" name="signup" type="submit" value="Signup">
            </td>
          </tr>
        </table>
      </form>

    【讨论】:

    • 太棒了!我现在收到遥控器的回复。唯一的问题是它总是没有被验证(它总是错误的)。错误消息“有效”或“无效”,取决于电子邮件是否有效。
    • 如果您使用上面的演示并提供与您提供的相同的电子邮件,它将为您提供有效的电子邮件,并且alertEmail is OK,这意味着如果您没有收到电子邮件,则会验证电子邮件然后您的脚本中的响应将您的代码与上面仔细比较。用户skyhawk133@gmail.com 在上面的电子邮件中并按注册。
    • 如果它适合你,请选择正确的答案@user2767441
    • 首先,非常感谢您的帮助!警报工作正常,我返回有效/无效。但是表单仍然不会提交,因为它将电子邮件字段视为错误。即使我“运行代码片段”的代码,它也是一样的。
    猜你喜欢
    • 1970-01-01
    • 2018-07-30
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    • 2016-08-02
    相关资源
    最近更新 更多