验证表单中的多个字段 [英] Validating multiple fields in a form

查看:56
本文介绍了验证表单中的多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次进行测试.我阅读了代码,并以此为自己的代码.事实是,即使字段为空也不会给出任何错误.

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

这是我的小提琴.

请帮忙.谢谢.

<!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>

推荐答案

您的大括号应位于函数validateForm()之后,if之后以及函数末尾.总体而言,在此示例中,括号是用螺钉拧紧的.

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.

将您的代码进行布局,以使左括号和右括号匹配并对您有意义.

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

这篇关于验证表单中的多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆