Javascript 验证表单中的多个字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10856887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:15:25  来源:igfitidea点击:

Validating multiple fields in a form

javascriptformshtmlvalidation

提问by fuddin

I was doing the testing for the first time. I read the thiscode and made one of my own from it. The thing is that its not giving any error even if the fields are left empty.

我是第一次做测试。我阅读了这段代码并从中制作了一个我自己的代码。问题是,即使字段为空,它也不会给出任何错误。

Here is my fiddle.

这是我的小提琴

Please help out. Thanks.

请帮忙。谢谢。

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
{function validateForm()

var x=document.forms["myForm"]["name"].value;
if (x==null || x=="")
  {
  alert("Name must be filled out");
  return false;
  }

var y=document.forms["myForm"]["password"].value;
  {
if (y==null || y=="")
  alert("Password name must be filled out");
  return false;
  }
</script>
</head>

<body>

<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
Name*: <input type="text" name="name"> <br>
Password*: <input type="password" name="password"><br>
Email: <input type="text" name="email"><br>
<input type="submit" value="Submit">
</form>
</body>

</html>

?

?

回答by glarkou

Fixed code: jsfiddle

固定代码:jsfiddle

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function validateForm() {

var x=document.forms["myForm"]["name"].value;
if (x==null || x=="")
 {
  alert("Name must be filled out");
  return false;
 }

var y=document.forms["myForm"]["password"].value;
if (y==null || y=="") {
  alert("Password name must be filled out");
  return false;
}
}
</script>
</head>

<body>

<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
Name*: <input type="text" name="name"> <br>
Password*: <input type="password" name="password"><br>
Email: <input type="text" name="email"><br>
<input type="submit" value="Submit">
</form>
</body>
<html>

Be careful of where you place your braces. Additionally, it is advantageous to use the console on your browser to identify some errors and fixed them.

小心放置牙套的位置。此外,使用浏览器上的控制台来识别一些错误并修复它们是有利的。

?

?

回答by Jason Stackhouse

You missed some braces {}and one was in the wrong spot.

你错过了一些大括号{},其中一个在错误的位置。

Hope this works:

希望这有效:

function validateForm() {
var x=document.forms["myForm"]["name"].value;

if (x==null || x=="")
  {
  alert("Name must be filled out");
  return false;
  }

var y=document.forms["myForm"]["password"].value;
  {
if (y==null || y=="")
  alert("Password name must be filled out");
  return false;
 }
}

回答by Jeff Watkins

Your brace should be after function validateForm() and after the if, and at the end of the function. Overall, the braces are screwed in this example.

你的大括号应该在函数 validateForm() 之后和 if 之后,以及函数的末尾。总的来说,在这个例子中大括号是拧紧的。

Lay your code out so the opening and closing braces match up and make sense to you.

把你的代码放好,这样左大括号和右大括号匹配起来,对你有意义。

回答by Amrut Jadhav

You misplaced the braces { }for validation of password. Place them after ifclause.

您放错了{ }用于验证密码的大括号。将它们放在if子句之后。

回答by Giorgos Tsakonas

I found it on Internet after a long time searching.. But it works just perfect..

经过长时间的搜索,我在互联网上找到了它..但它工作得非常完美..

The html code

html代码

<html>
<head>
<title>Form Validation</title>
<script type="text/javascript">
<!--
// Form validation code will come here.
//-->
</script>
</head>
<body>
 <form action="/cgi-bin/test.cgi" name="myForm"  
          onsubmit="return(validate());">
 <table cellspacing="2" cellpadding="2" border="1">
 <tr>
   <td align="right">Name</td>
   <td><input type="text" name="Name" /></td>
 </tr>
 <tr>
   <td align="right">EMail</td>
   <td><input type="text" name="EMail" /></td>
 </tr>
 <tr>
   <td align="right">Zip Code</td>
   <td><input type="text" name="Zip" /></td>
 </tr>
 <tr>
 <td align="right">Country</td>
 <td>
 <select name="Country">
   <option value="-1" selected>[choose yours]</option>
   <option value="1">USA</option>
   <option value="2">UK</option>
   <option value="3">INDIA</option>
 </select>
 </td>
 </tr>
 <tr>
   <td align="right"></td>
   <td><input type="submit" value="Submit" /></td>
 </tr>
 </table>
 </form>
 </body>
 </html>

the javascript

javascript

<script type="text/javascript">
<!--
// Form validation code will come here.
function validate()
{

   if( document.myForm.Name.value == "" )
   {
     alert( "Please provide your name!" );
     document.myForm.Name.focus() ;
     return false;
   }
   if( document.myForm.EMail.value == "" )
   {
     alert( "Please provide your Email!" );
     document.myForm.EMail.focus() ;
     return false;
   }
   if( document.myForm.Zip.value == "" ||
           isNaN( document.myForm.Zip.value ) ||
           document.myForm.Zip.value.length != 5 )
   {
     alert( "Please provide a zip in the format #####." );
     document.myForm.Zip.focus() ;
     return false;
   }
   if( document.myForm.Country.value == "-1" )
   {
     alert( "Please provide your country!" );
     return false;
   }
   return( true );
}
//-->
</script>

and the function for email validation

和电子邮件验证功能

<script type="text/javascript">
<!--
function validateEmail()
{

   var emailID = document.myForm.EMail.value;
   atpos = emailID.indexOf("@");
   dotpos = emailID.lastIndexOf(".");
   if (atpos < 1 || ( dotpos - atpos < 2 )) 
   {
       alert("Please enter correct email ID")
       document.myForm.EMail.focus() ;
       return false;
   }
   return( true );
}
//-->
</script>

you can check it online here

你可以在这里在线查看