1

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>
2
  • 1
    You might want to start closing your input tags Commented Mar 11, 2014 at 12:20
  • why u dont use jquery? Commented Mar 11, 2014 at 12:23

2 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

7 Comments

If i Change Like this When ever I press Cancel Button it Shows an alert and also for Forgot Password.
Have you changed <input type="button"> to <input type="submit"> for the login button only?
No I changed for all the Buttons.
Just do it for the register button only and it will solve your problem
If you want to write code for cancel button make it type="button" and call some function on it's on-click method and do your code in that function. Simple!!!
|
0

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

2 Comments

He has HTML5 doctype so it's not necessary to close your input tags though
@DieterGoetelen Right. I agree. However, as a coding convention and good code practice it advisable to close it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.