简体   繁体   English

关闭某些字段的表单验证

[英]Turn off Form Validation for certain fields

I've a simple form that performs validation before user is able to proceed to the next tab.我有一个简单的表单,可以在用户能够继续到下一个选项卡之前执行验证。

<h1>Register:</h1>
<!-- One "tab" for each step in the form: -->
<div class="tab">Name:
  <p><input name="fname"></p>
  <p><input name="lname"></p>
</div>
<div class="tab">Contact Info:
  <p><input name="email"></p>
  <p><input name="phone"></p>
</div>
<div class="tab">Birthday:
  <p><input name="dd"></p>
  <p><input name="nn"></p>
  <p><input name="yyyy"></p>
</div>

The script used to validate is this:用于验证的脚本是这样的:

function validateForm() {
  // This function deals with validation of the form fields
  var x, y, i, valid = true;
  x = document.getElementsByClassName("tab");
  y = x[currentTab].getElementsByTagName("input");
  // A loop that checks every input field in the current tab:
  for (i = 0; i < y.length; i++) {
    // If a field is empty...
    if (y[i].value == "") {
      // add an "invalid" class to the field:
      y[i].className += " invalid";
      // and set the current valid status to false
     valid = false;
    }
  }
  // If the valid status is true, mark the step as finished and valid:
  if (valid) {
    document.getElementsByClassName("step")[currentTab].className += " finish";
  }
  return valid; // return the valid status
}

However, not all fields need validation.但是,并非所有字段都需要验证。 How can I "turn off" validation for the phone input via the script?如何通过脚本“关闭”电话输入验证?

I'm not sure if by 'invalid' you mean just empty fields.我不确定您所说的“无效”是否只是空字段。 If so, you can use the html attribute required to handle empty fields like this:如果是这样,您可以使用 html 属性来处理这样的空字段:

<input name="email" required />

You can also add an additional condition that checks if the name attribute of the element is 'phone' to skip it.您还可以添加一个附加条件来检查元素的名称属性是否为“电话”以跳过它。

Currently the validateForm function is just checking for empty fields, so you can just use the required attribute, as stated by Kent.目前validateForm function 只是检查空字段,因此您可以只使用required属性,如 Kent 所述。

If you are doing other stuff in the validation, you can add a class="no-validate" to the specific input you don't want to validate and in your JavaScript, you select only the input that does not have the class "no-validate" .如果您在验证中做其他事情,您可以将class="no-validate"添加到您不想验证的特定输入,在您的 JavaScript 中,您 select 只有没有 class 的输入"no-validate"

function validateForm () {
    //...
    y = x[currentTab].querySelectorAll("input:not(.no-validate)");
    //
}

Or you can do it the other way by rather giving a validate class to all the inputs that needs validation and select them in your code with:或者你可以用另一种方式来做,而不是给所有需要验证的输入一个validate class 并在你的代码中给它们 select :

y = x[currentTab].getElementsByClassName("validate");

Apart from that, before you take time to implement any custom validation, you can make use of the form validation that is already there for you in HTML5. You can learn more about it here除此之外,在您花时间实施任何自定义验证之前,您可以使用 HTML5 中已有的表单验证。您可以在此处了解更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM