即使没有满足所有规则,jquery验证器仍会继续

时间:2016-02-13 08:21:03

标签: javascript php jquery html validation

我有基本的HTML,并希望用jQuery验证器验证字段,所以我在php级别上的验证较少。表单验证所有字段是否为空并且阻止提交到php但是一旦我完成1输入表单求和(即使所有其他字段都是空白)。所有字段都是必填项,所以我很难过 - 请帮助!

根据你的一些建议,我已经在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']);
   }
  }
 }

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

在html的标题上需要jquery文件链接。

答案 2 :(得分:0)

也许是因为你没有使用字段集封闭? 另一方面,您可以看一下简单的HYML5验证。