简体   繁体   English

如何在Javascript中进行表单验证

[英]How to do Form Validation in Javascript

I Write This code For Registration of a User. 我写这个代码用于注册用户。 Now I want to Validate this Page Whether all the Fields are Filled by the user or Not. 现在我想验证此页面是否所有字段都由用户填写或不填写。 The Same Script Code I Write to Login page It's Showing an alert if any one filed empty in the page. 我写入登录页面的相同脚本代码如果页面中任何一个空白,则显示警告。 But it is not working for this Page. 但它不适合这个页面。

The Main Difference is i Used Div in Login Page here I Used Table. 主要区别是我在登录页面使用Div我在这里使用表。

Registration Page Code : 注册页码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="css/Registration.css">
<script>
function validateForm()
{
var a=document.forms["regform"]["user"].value;
var b=document.forms["regform"]["pass"].value;
var c=document.forms["regform"]["copa"].value;
var d=document.forms["regform"]["mono"].value;
if(a==""||b==""||c=""||d="")
{
    alert("Please Enter All the Fields");
    return false;
}
else
{
    window.location="afterregister.html"
    //alert("Registered Successfully");
}
 }
</script>
</head>
<body>
<h1>Registration</h1>
<form name="regform" onsubmit="return validateForm();" method="post">
<table>
<tr>
    <td><label>Username:</label></td>
    <td><input type="text" name="user"/></td>
</tr>
<tr>
    <td><label>Password:</label></td>
    <td><input type="text" name="pass"/></td>
</tr>
<tr>
    <td><label>Confirm Password:</label></td>
    <td><input type="text" name="copa"/></td>
</tr>
<tr>
    <td><label>Mobile Number:</label></td>
    <td><input type="text" name="mono"/></td>
</tr>
<tr>
<td></td>
    <td><input type="button" onclick = "return validateForm()" value="Register" name="" class="row1" />
    <input type="button" name="" value="Cancel" class="row1"></td>
</tr>
<tr>
<td></td>
    <td><input type="button" name="" value="Forgot Password" class="row2"></td>
</tr>
    </table>
</form>
</body>
</html>

Login Page Code: 登录页码:

<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="css/Login.css">
<script>
function myFunction()
{
var x=document.forms["loginform"]["user"].value;
var y=document.forms["loginform"]["pass"].value;
if(x==""||y=="")
{
    alert("Please Enter All the Fields");
    return false;
}
else
{
    window.location="afterLogin.html"
}
}
</script>
</head>
<div></div>
<body>
<h1></h1>
    <form name="loginform"  onsubmit="return myFunction();" method="post">
    <div class="username"><label>Username:</label><input type="text" name="user"/></div>
    <div class="password"><label>Password:</label><input type="text" name="pass"/></div>
    <div class="buttons"><input type="button" onclick = "return myFunction()" value="Login" name=""/> <input type="button" value="Cancel" name=""/></div>
</form>

</body>

You are already calling the validation function on your form submit onsubmit="return myFunction();" 您已经在表单上调用验证函数提交onsubmit="return myFunction();" . So there isn't any need to call it again on your button click . 因此,无需再次button click再次调用它。 So please change 所以请改变

 <input type="button" onclick = "return validateForm()" value="Register" name="" class="row1" />

to

 <input type="submit" value="Register" name="" class="row1" />

And do this in your script 并在您的脚本中执行此操作

<script>
function validateForm()
{
var a=document.forms["regform"]["user"].value;
var b=document.forms["regform"]["pass"].value;
var c=document.forms["regform"]["copa"].value;
var d=document.forms["regform"]["mono"].value;
if(a===""||b===""||c===""||d==="")
{
    alert("Please Enter All the Fields");
    return false;
}
else
{
    window.location="afterregister.html"
    //alert("Registered Successfully");
}
 }
</script>

Solution: Do the following 2 steps: 解决方案:执行以下两个步骤:

1- First close your <input> tags as either of the following method: 1-首先按以下方法之一关闭<input>标记:

i. 一世。 <input type = "textbox" name = "name" /> <!-- This is suggested -->

OR 要么

ii. II。 <input type = "textbox" name = "name"></input>

2- Go through these 2 useful links - it will guide you through what you exactly need: 2-浏览这两个有用的链接 - 它将指导您完成您的确切需求:

Form Validation Tutorial - 1 表单验证教程 - 1

Form Validation Tutorial - 2 表格验证教程 - 2

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

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