Check out the live demo here
HTML Form Structure
<form name="userDetails">
<table cellspacing="1" cellpadding="1"&tg;
<tr>
<td>Name: </td>
<td><input type='text' name='Name'/>
<span class="error_span">Please enter your name</span>
</td>
</tr>
<tr>
<td>Last Name: </td>
<td><input type='text' name='LastName'/>
<span class="error_span">Please enter your last name</span>
</td>
</tr>
<tr>
<td>Address: </td>
<td><input type='text' name='Address'/>
<span class="error_span">Please enter your address</span>
</td>
</tr>
</table>
<input type='button' id='submitForm' name='enter' value='Submit'/>
</form>
jQuery Validation Script
$(document).ready(function(){
$("#submitForm").click(function(){
$('input').each(function(index) {
if($(this).val()=="") {
$(this).addClass("has_error");
$(this).siblings(".error_span").show();
}else{
$(this).removeClass("has_error");
$(this).siblings(".error_span").hide();
}
});
});
});
CSS Styling for Error Messages
.error_span{
color:red;
display:none;
}
.has_error{
border:1px solid red;
}
View the demo on JSFiddle
To implement form validation, use the onsubmit
attribute in the form element to run the JavaScript code and prevent submission if there are validation errors.