If you are using jQuery (if not, you should) you can do something when the form gets submitted through .submit()
. Within this event you can check all the input
fields depending on their type, if an error occurs you can call event.preventDefault()
to stop the submitting and alert()
an error.
Something like this:
<form id="myform">
<input type="text" name="text" />
<input type="submit" value="Submit!" />
</form>
jQuery:
$('#myform').submit(function(event){
error = false;
$('input',this).each(function(){
if($(this).val() == '')
error = true;
});
if(error == true){
event.preventDefault();
alert('An error occured!');
}
});