简体   繁体   English

用JavaScript进行表单验证。 不返回期望的结果

[英]Form Validation with JavaScript . Not Returning Desired Result

i'm making a sign up form but having some trouble checking all the required fields 我正在注册表格,但在检查所有必填字段时遇到了一些麻烦

HTML 的HTML

<form id="form">
  <div class="form-group" id="form-group">
    <label for="exampleInputEmail1">Username</label>
    <input type="text" class="form-control" id="username" placeholder="username">
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Email</label>
    <input type="email" class="form-control" id="email" placeholder="email">
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="password" placeholder="Password">
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Confirm Password</label>
    <input type="password" class="form-control" id="confirm_password" placeholder="Confirm Password">
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Message</label>
    <textarea class="form-control" rows="10" id="message"></textarea>
  </div>
  <div class="checkbox">
    <label class="checkbox-inline">
      <input type="checkbox" id="checkbox1" value="option1">0
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="checkbox2" value="option1">1
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="checkbox3" value="option2">2
    </label>
    <label class="checkbox-inline">
      <input type="checkbox" id="checkbox4" value="option3">3
    </label>
  </div>
  <br/>
  <p><span id="messagealert"></span>
  </p>
  <button type="submit" class="btn btn-default">Submit</button>
</form>

JavaScript 的JavaScript

function checkForm() {

   var formSubmit = document.getElementById("form");
   formSubmit.onsubmit = function() {
     var username = document.getElementById("username");
     var email = document.getElementById("email");
     var password = document.getElementById("password");
     var confirm_password = document.getElementById("confirm_password");
     var message = document.getElementById("message");
     var messageAlert = document.getElementById("messagealert");

     var checkbox1 = document.getElementById("checkbox1");

     if (username.value == "" && email.value == "" && password.value == "" && confirm_password.value == "" && message.value == "") {
       messageAlert.innerHTML = "Please fill out the fields";
       username.style.border = "1px solid red";
       email.style.border = "1px solid red";
       password.style.border = "1px solid red";
       confirm_password.style.border = "1px solid red";
       message.style.border = "1px solid red";
       return false;

     } else {
       return true;
     }

   };

 }

 window.onload = function() {
   checkForm();
 }

Now when all the fields are empty it returns false but i fill one of the fields its returning me true. 现在,当所有字段都为空时,它返回false,但是我填充其中一个字段,从而返回true。 i don't whats wrong with my code and also please tell me how to make check on checkboxes also 我的代码没什么问题,也请告诉我如何对复选框进行检查

Yes that is because you if if (username.value == "" && email.value == "" && password.value == "" && confirm_password.value == "" && message.value == "") isn't true when one of these values are filled in therefore you will hit the else. 是的,这是因为如果您if (username.value == "" && email.value == "" && password.value == "" && confirm_password.value == "" && message.value == "") '当填入这些值之一时,t为真,因此您将击中其他值。

try something like this: 尝试这样的事情:

var x = 1;
if (username.value == ""){
   x = 0;
   username.style.border = "1px solid red";
}
if (email.value == ""){
   x = 0;
   email.style.border = "1px solid red";
}
if (password.value == ""){
   x = 0;
   password.style.border = "1px solid red";
}
if (confirm_password.value == ""){
   x = 0;
   confirm_password.style.border = "1px solid red";
}
if (message.value == ""){
   x = 0;
   message.style.border = "1px solid red";
}

if (x === 1){
   return true;
} else {
   messageAlert.innerHTML = "Please fill out the fields";
   return false;
}

This might be a terrible solution, you could add all those selectors into an array or similar and foreach them out and check their values to determine if they are set or not. 这可能是一个糟糕的解决方案,您可以将所有这些选择器添加到数组或类似物中,并将它们选择出来,然后检查它们的值以确定是否设置了它们。 But anyway this will make you move forward atleast. 但是无论如何,这将使您至少向前迈进。

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

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