0
   

Java Script Form Validation

 
 
Reply Thu 26 Jun, 2014 08:35 am
Hi! I posted my code below. Im having a problem in my js form validation. When i leave my first name empty it displays the message(in red) "first name must be filled out"(so far so good) .Next then when i type in something it gives me" valid input"(in green). The problem begins when i erase the first name and submit it again the "valid input" does not go away.(its like the code is only moving forward). Please Help!
The css is at the bottom.





<!doctype html>
<html lang="en">
<meta charset="utf-8">
<html>

<head>
<link rel="script" href="js/index1.js">
<link rel="stylesheet" href="index1.css">
<script type="text/javascript">

function validator () {


var x = document.hello.textbox
var y = document.hello.lname

if (x.value.length == 0)
{
document.getElementById("fname").style.display="";
return false;
}
else
document.getElementById("greenvalid").style.display="";
return true;
}
</script>
<title>Form Validation.</title>

</head>

<body>
<div class="imagecard">
` <img src="index_card.png" width"450px" height="300px"/>
</div>


<div class="image">
` <img src="whoeei.png" />
</div>

<form name="hello">

<div class="fname">

First Name: &nbsp &nbsp <input type="text" placeholder="John"name="textbox"/>3 to 10 Characters required<br>

<div id="fname" style="color:Red;display:none">First name must be filled out:</div>

<div id="threeormore" style="color:Red;display:none">First name must have 3 or more letters:</div>

<div id="greenvalid" style="color:green;display:none">Valid input: &nbsp &nbsp &nbsp &nbsp &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp </div>


<div id="leastten1" style="color:Red;display:none">First name must have less than 10 letters:</div>

</div>

<div class="lname">

Last Name:&nbsp &nbsp &nbsp<input type="text" placeholder="smith" name="lname"/>3 to 10 Characters required<br>

<div id="llname" style="color:Red;display:none"> Last name must be filled out:</div>

<div id="threeormore2" style="color:Red;display:none">Last name must have 3 or more letters:</div>

<div id="leastten2" style="color:Red;display:none">Last name must have less than 10 letters:</div>

</div>

<div id="ffname" style="color:Red;display:none">Last Name must be filled out:</div>
<div class="email">
Email:&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp&nbsp <input type="email" placeholder="[email protected]" name="email"/>Required<br>
<div id="email" style="color:Red;display:none">Valid Email must be filled out:</div>
</div>

<div class="submit">
<input type="button" value="Submit" onClick="validator()">
</div>
</div>


</form>



</body>

</html>




css
html{
background-color:white;
}
.fname{
position: absolute;
top:56px;
left:20px;
}
.imgcard{
position:absolute;

}
.lname{
position: absolute;
top:107px;
left:20px;

}
.email{
position: absolute;
top:157px;
left:20px;
}
.submit{
position: absolute;
top:233px;
left:400px;
}
.image{
position:absolute;
top:50px;
left:400px;

}
#fname{
position:absolute;
top:130px;
left:20px;
background-color: white;
}

#greenvalid{
position:absolute;
top:130px;
left:20px;
background-color: white;
}





#llname{
position:absolute;
top:105px;
left:20px;
background-color: white;
}

#email{
position:absolute;
top:80px;
left:20px;
background-color: white;
}

#threeormore{
position:absolute;
top:130px;
left:20px;
background-color: white;
}
#threeormore2{
position:absolute;
top:105px;
left:20px;
background-color: white;
}

#leastten1{
position:absolute;
top:130px;
left:20px;
background-color: white;
}

#leastten2{
position:absolute;
top:105px;
left:20px;
background-color: white;
}
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 0 • Views: 552 • Replies: 0
No top replies

 
 

Related Topics

 
  1. Forums
  2. » Java Script Form Validation
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 05/16/2024 at 11:33:47