Course Banner


JavaScript Form validation

<script>

function frmVaidate(){

if(document.getElementById('f_name').value==''){

alert('Please enter your first name');
document.getElementById('f_name').focus();
return false;
}
else if(document.getElementById('l_name').value==''){

alert('Please enter your last name');
document.getElementById('l_name').focus();
return false;
}

else if(document.getElementById('email').value==''){

alert('Please enter your email');
document.getElementById('email').focus();
return false;
}

else if(document.getElementById('password').value==''){

alert('Please enter Password');
document.getElementById('password').focus();
return false;
}

else if(document.getElementById('password2').value==''){

alert('Please re-enter your password');
document.getElementById('password2').focus();
return false;
}

else if(document.getElementById('password').value!=document.getElementById('password2').value){

alert('Both the passwords are no same.');
document.getElementById('password2').focus();
return false;
}

 

else if(document.getElementById('address').value==''){

alert('Please enter your addres.');
document.getElementById('address').focus();
return false;
}

else if(document.getElementById('address').value==''){

alert('Please enter your address.');
document.getElementById('address').focus();
return false;
}

else if(document.getElementById('male').checked!=true && document.getElementById('female').checked!=true ){

alert('Please select your geneder');
document.getElementById('male').focus();
return false;
}

else if(document.getElementById('movies').checked!=true && document.getElementById('cricket').checked!=true && document.getElementById('reading').checked!=true && document.getElementById('swimming').checked!=true ){

alert('Please check your hobbies');
document.getElementById('movies').focus();
return false;
}

else if(document.getElementById('country').value==0){

alert('Please select your country');
document.getElementById('country').focus();
return false;
}

}

</script>

<form method="post" action="" name="frm_register" onsubmit="return frmVaidate();">
<table width="600" border="1" cellspacing="5">
<tr>
<td>First Name</td>
<td><input type="text" name="f_name" id="f_name"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="l_name" id="l_name"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="email" name="email" id="email"></td>
</tr>

<tr>
<td>Password</td>
<td><input type="password" name="password" id="password"></td>
</tr>
<tr>
<td>Re-type Password</td>
<td><input type="password" name="password2" id="password2"></td>
</tr>

<tr>
<td>Address</td>
<td><textarea name="address" id="address"></textarea></td>
</tr>

<tr>
<td>Gender</td>
<td>Male <input type="radio" name="gender" id="male" value="male"> Female<input type="radio" name="gender" id="female" value="female"></td>
</tr>

<tr>
<td>Hobbies</td>
<td>
Movies : <input type="checkbox" name="movies" value="movies" id="movies">
Reading : <input type="checkbox" name="reading" value="reading" id="reading">
Cricket : <input type="checkbox" name="cricket" value="cricket" id="cricket">
Swimming : <input type="checkbox" name="swimming" value="swimming" id="swimming">
</td>
</tr>

<tr>
<td>Country</td>
<td>
<select id="country">
<option value="0" selected="selected">Select Country</option>
<option value="india">India</option>
<option value="pakistan">pakistan</option>
<option value="china">China</option>
<option value="Shrilanka">Shrilanka</option>
</select>
</td>
</tr>

<tr>
<td></td>
<td><input type="submit" name="submit" id="Submit Form"></td>
</tr>

</table>
</form>