【问题标题】:jquery validator continues even though all rules have not been met即使没有满足所有规则,jquery 验证器也会继续
【发布时间】:2016-02-13 08:21:03
【问题描述】:

我有基本的 html,想用 jQuery 验证器验证字段,所以我在 php 级别上的验证较少。该表单验证所有字段是否为空并阻止提交到 php,但一旦我完成 1 输入表单 sumits(即使所有其他字段为空)。所有字段都是必需的,所以我很难过 - 请帮忙!

根据您的一些建议,我已经在 php 级别上重新进行了验证,但效果完全相同。如果所有字段都为空,则验证工作,只要在表单中填写 1 个字段即可提交。

jQuery:我通过 JSLint 运行它,它没有产生任何错误 - 难以置信,对吧?!

$().ready(function () {
  "use strict";
  $('#register').validate({

rules: {
  firstname: {
    required: true,
    maxLength: 40
  },
  lastname: {
    required: true,
    maxLength: 40
  },
  email: {
    required: true,
    maxLength: 64,
    email: true
  },
  password: {
    required: true,
    minLength: 6,
    maxLength: 32
  },
  confirmPassword: {
    required: true,
    equalTo: "#password"
  },
  rsaid: {
    required: true,
    digits: true
  }
},

messages: {
  firstname: {
    required: "Please enter your first name.",
    maxLength: "Your first name cannot exceed 40 characters."
  },
  lastname: {
    required: "Please enter your last name.",
    maxLength: "Your last name cannot exceed 40 characters."
  },
  email: {
    required: "Please enter your email address.",
    maxLength: "Your email address cannot exceed 64 characters.",
    email: "The email format provided is invalid."
  },
  password: {
    required: "Please enter a password.",
    minLength: "Your password must contain at least 6 characters.",
    maxLength: "Your password cannot contain more than 32 characters."
  },
  confirmPassword: {
    required: "Please confirm your password.",
    equalTo: "Your passwords do not match!"
  },
  rsaid: {
    required: "Please enter a valid RSA id number.",
    //exactLength: "Your ID number must contain 13 characters!",
    digits: "Your ID number must consist of numerals only!"
  }
},

errorContainer: $('#errorContainer'),
errorLabelContainer: $('#errorContainer ul'),
wrapper: 'li'

});
});

html:不应该,但以防万一:)

<div class="registrationForm">
        <form id="register" action="php/insert.php" method="post">
        <input type="text" id="firstname" name="firstname" placeholder="First Name" value="" class="radius mini" />
        <input type="text" id="lastname" name="lastname" placeholder="Last Name" value="" class="radius mini"/>
        <input type="text" id="email" name="email" placeholder="Your Email" value="" class="radius" />
        <input type="password" id="password" name="password" placeholder="New Password" value="" class="radius" />
        <input type="password" id="confirmPassword" name="confirmPassword" placeholder="Confirm Password" value="" class="radius" />
        <input type="text" id="rsaid" name="rsaid" placeholder="RSA ID Number" value="" class="radius" />
        <button class="radius title" name="signup">Sign Up for SFC</button>
    </form>
</div>

PHP 代码:这仅包含前 3 个字段的代码,因为密码验证很长且无关紧要。该代码在 phpcodechekcer.com 上没有返回错误。

if ($_SERVER["REQUEST_METHOD"] == "POST") {

  if (empty($_POST["firstname"])) {
  $firstnameErr = "First name is required";
} else {
$firstname = mysqli_real_escape_string($link, $_POST['firstname']);
  }

 if (empty($_POST["lastname"])) {
  $lastnameErr = "Last name is required";
  } else {
  $lastname = mysqli_real_escape_string($link, $_POST['lastname']);
 }

  if (empty($_POST["email"])) {
   $emailErr = "Email address is required";
   } else {
  if (!isValidEmail($_POST["email"])) {
    $emailErr = "Email address is invalid";
   } else {
    $email = mysqli_real_escape_string($link, $_POST['email']);
   }
  }
 }

【问题讨论】:

  • 我建议您使用php进行验证。如果用户关闭js并在文本框中添加恶意代码(XSS),您的网站很可能会被黑客入侵

标签: javascript php jquery html validation


【解决方案1】:

最好在服务器端进行验证,如果您的客户端在浏览器上关闭了 javascript,那么所有数据都将发送到服务器而无需任何验证

【讨论】:

  • 是的……如果打开了 javascript,这对最终用户来说会更加方便和美观。
【解决方案2】:

在html的头部需要jquery文件链接。

【讨论】:

  • 我应该将它添加到代码中,我非常喜欢 jquery 1.12.0 和 jquery.validator 1.14.0 以及我的脚本。如果我运行一个简单的 jQuery 警报,它就可以工作。
【解决方案3】:

也许是因为你没有使用 fieldset 封闭? 另一方面,您可能会考虑简单的 HYML5 验证。

【讨论】:

  • 当使用表单时,字段集似乎不是必需的stackoverflow.com/questions/658208/…
  • 您的问题在于 minLength / maxLength 的命名 - jquery.validate 在尝试调用这些方法时抛出错误 - 您应该使用 minlength(注意大小写)和 maxlength。见jsfiddle.net/L6ca31ek
  • 哇,谢谢你成功了!正在尽我所能,这个案子是我的问题:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 2021-03-11
  • 2018-03-31
相关资源
最近更新 更多