简体   繁体   English

使用JavaScript验证表单?

[英]validate form with javascript?

I am trying to use the following code to validate my form before a user can submit. 我正在尝试使用以下代码在用户可以提交之前验证我的表单。

javascript: javascript:

<script>
    function validateForm() {
     var x = document.forms["register"]["firstname"].value;
     var x = document.forms["register"]["lastname"].value;
     var x = document.forms["register"]["email"].value;
     var x = document.forms["register"]["email2"].value;
     if (x == null || x == "") {
         $(".form_error").show();
         return false;
     }
 }
 </script>

my form basically looks like this: 我的表格基本上是这样的:

Section 1: 第1节:

<form name="register" action="include/signup_process.php" onsubmit="return validateForm()" method="post">

<div class="form_error">Ooops! There is some missing or incorrect information. Please look back over this section. </div> 

<input type="text" name="firstname" class="login_form2"><br/>
<input type="text" name="lastname" class="login_form2"><br/> 

Section 2 第二节

<form name="register" action="include/signup_process.php" onsubmit="return validateForm()" method="post">

<div class="form_error2">Ooops! There is some missing or incorrect information. Please look back over this section. </div> 

<input type="text" name="email" class="login_form2"><br/>
<input type="text" name="email2" class="login_form2"><br/>  

Section 3 第三节

    <form name="register" action="include/signup_process.php" onsubmit="return validateForm()" method="post">

    <div class="form_error3">Ooops! There is some missing or incorrect information. Please look back over this section. </div> 

    <input type="text" name="username" class="login_form2"><br/>
    <input type="text" name="password" class="login_form2"><br/>  

I have basically broken my form into three sections, and when a user submits the form they are suppose to get one of 3 possible errors/divs which tell the user which section they need to go back and revise. 我基本上将表单分为三个部分,并且当用户提交表单时,他们假设会得到3个可能的错误/ div之一,该错误/ div告诉用户他们需要返回哪个部分并进行修改。

I am trying to amend my javascript code like below so that I can basically run a different check for the input boxes in my form for each section and display the different div elements depending on which input box from the appropriate section causes the error. 我正在尝试像下面那样修改我的JavaScript代码,以便基本上可以对表单中的每个部分的输入框进行不同的检查,并根据相应部分中的哪个输入框会导致错误显示不同的div元素。 can someone please show me what I would need to do to make this work. 有人可以告诉我如何进行这项工作吗? thanks in advance. 提前致谢。

<script>
        function validateForm() {
         var x = document.forms["register"]["firstname"].value;
         var x = document.forms["register"]["lastname"].value;
         if (x == null || x == "") {
         $(".form_error").show();
         return false;
         var x = document.forms["register"]["email"].value;
         var x = document.forms["register"]["email2"].value;
         if (x == null || x == "") {
             $(".form_error2").show();
             return false;
         var x = document.forms["register"]["username"].value;
         var x = document.forms["register"]["password"].value;
         if (x == null || x == "") {
             $(".form_error2").show();
             return false;



         }
     }
     </script>

Every time you do this: 每次您这样做:

var x = document.forms["register"]["firstname"].value;
var x = document.forms["register"]["lastname"].value;

you're (needlessly) redeclaring x , and throwing away the first field's value. 您(不必要)重新声明x ,并丢弃第一个字段的值。 It will never be checked. 它将永远不会被检查。

You're also missing a number of closing } s. 您还缺少多个结束}

Try something like: 尝试类似的方法:

function validateForm() {
  var x = document.forms["register"]["firstname"].value;
  var y = document.forms["register"]["lastname"].value;
  if ((! x) || (! y)) {     // "! x" covers both null and "" cases
    $(".form_error").show();
    return false;
  } 

  x = document.forms["register"]["email"].value;
  y = document.forms["register"]["email2"].value;
  if ((! x) || (! y)) {     
    $(".form_error2").show();
    return false;
  }

  x = document.forms["register"]["username"].value;
  y = document.forms["register"]["password"].value;
  if ((! x) || (! y)) {     
    $(".form_error2").show();
    return false;
  }
}

Well this may just be me, but you are overriding the value of x repeatedly here: 好吧,这可能只是我,但是您在这里反复重写x的值:

var x = document.forms["register"]["firstname"].value;
var x = document.forms["register"]["lastname"].value;
var x = document.forms["register"]["email"].value;
var x = document.forms["register"]["email2"].value;

it looks to me as if x would then be given a new value every time and then would only contain the last value it was asigned. 在我看来,好像x每次都会被赋予一个新值,然后只包含它被赋值的最后一个值。 Essentially, if the last box is filled in, the form will be counted "valid". 本质上,如果填写了最后一个框,则该表单将被视为“有效”。 Make an array and store each value into its own index, then use a for loop to go through them and check if any are equal to null or an empty string. 创建一个数组并将每个值存储到自己的索引中,然后使用for循环遍历它们,并检查是否等于null或空字符串。

var x = [];
x[0] = document.forms["register"]["firstname"].value;
x[1] = document.forms["register"]["lastname"].value;
x[2] = document.forms["register"]["email"].value;
x[3] = document.forms["register"]["email2"].value;

x.forEach(function(element, index, array) {

if (element == null || element == "") {

$(".form_error").show();
return false;

}

});

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

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