简体   繁体   中英

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.

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();" . So there isn't any need to call it again on your 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:

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

OR

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

2- Go through these 2 useful links - it will guide you through what you exactly need:

Form Validation Tutorial - 1

Form Validation Tutorial - 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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