function formValidation(oEvent) {
oEvent = oEvent || window.event;
var txtField = oEvent.target || oEvent.srcElement;
var t1ck=true;
var msg=" ";
if(document.getElementById("t1").value.length < 3 )
{ t1ck=false; msg = msg + "Your name should be minimun 3 char length";}
if(!document.getElementById("r1").checked && !document.getElementById("r2").checked)
{ t1ck=false;msg = msg + " Select your Gender";}
if(document.getElementById("s1").value.length < 3 )
{ t1ck=false;msg = msg + " Select one of the games ";}
if(!document.getElementById("c1").checked )
{ t1ck=false;msg = msg + " You must agree to terms & conditions ";}
//alert(msg + t1ck);
if(t1ck){document.getElementById("btnSignUp").disabled = false; }
else{document.getElementById("btnSignUp").disabled = true; }
}
You can see we have set a flag t1ck to true and then check condition of all the form elements, if the condition is failed then we are setting the flag t1ck to false. You can see we are also generating a message associated with each form elements. We linked this message to an alert message. We kept this alert message commented as this will display the status of all the messages for every change in any form element. This helps in development or learning environment but not in actual application level. So you can un-comment this line and know about the status of each validation while your are trying to learn the code. Here is the full code.
<html>
<head>
<script type="text/javascript" >
function formValidation(oEvent) {
oEvent = oEvent || window.event;
var txtField = oEvent.target || oEvent.srcElement;
var t1ck=true;
var msg=" ";
if(document.getElementById("t1").value.length < 3 )
{ t1ck=false; msg = msg + "Your name should be minimun 3 char length";}
if(!document.getElementById("r1").checked && !document.getElementById("r2").checked)
{ t1ck=false;msg = msg + " Select your Gender";}
if(document.getElementById("s1").value.length < 3 )
{ t1ck=false;msg = msg + " Select one of the games ";}
if(!document.getElementById("c1").checked )
{ t1ck=false;msg = msg + " You must agree to terms & conditions ";}
//alert(msg + t1ck);
if(t1ck){document.getElementById("btnSignUp").disabled = false; }
else{document.getElementById("btnSignUp").disabled = true; }
}
function resetForm(){
document.getElementById("btnSignUp").disabled = true;
var frmMain = document.forms[0];
frmMain.reset();
}
window.onload = function () {
var btnSignUp = document.getElementById("btnSignUp");
var btnReset = document.getElementById("btnReset");
var t1 = document.getElementById("t1");
var r1 = document.getElementById("r1");
var r2 = document.getElementById("r2");
var s1=document.getElementById("s1");
var c1=document.getElementById("c1");
var t1ck=false;
document.getElementById("btnSignUp").disabled = true;
t1.onkeyup = formValidation;
r1.onclick = formValidation;
r2.onclick = formValidation;
s1.onclick = formValidation;
c1.onclick = formValidation;
btnReset.onclick = resetForm;
}
</script>
</head>
<body >
<form method=" post" action="form-success.php">
<table>
<tr>
<td>First Name</td>
<td><input type="text" id="t1" name="FirstName"></td>
</tr>
<tr>
<tr>
<td>Gender</td>
<td><input type="radio" id="r1" name="gender" value=Male>Male
<input type="radio" id="r2" name="gender" value=Female>Female</td>
</tr>
<tr>
<td>Games Played</td>
<td><select name="games" id="s1" >
<option value=''>Select One</option><option value=Football>FootBall</option>
<option value=Tenis>Tenis</option>
<option value=cricket>Cricket</option>
</select>
</td>
</tr>
<tr><td>Terms & Condition</td><td><input type=checkbox name=agree value=yes id=c1></td></tr>
</table>
<input type="submit" id="btnSignUp" value="Sign Up!!" /> <input type=reset id=btnReset value=Reset>
</form>
</body>
</html>
Steffan | 04-08-2010 |
And what do i have to change if I use a textarea :D ? |
Mashal Khan | 25-05-2012 |
very knowledgeable and helpful for JavaScript coders. Excellent. Fantastic. |
Vvk | 11-02-2013 |
Really helped! Thanks!! |
riya | 14-07-2015 |
helped a lot |
Hirdey | 08-09-2015 |
Working on demo. But not working on my site. Please help me |
fashn | 30-06-2016 |
Thanks - it's just what I was looking for! |