javascript form validation
Posted: Thu Jul 29, 2010 3:44 am
hi guys, i've got a form validation java script that i found online to make sue fields are typed in before going onto the next field, this works fine but myproblem is that if i try to fill in the form but miss the forname field leaving it blank and pressing tab to continue onto the surname field it gives the alert saying that you need to fill in the forenames field, great, but then as i had pressed tab it then gives the alert to fill in the surnmae field, and then loops between them as i have used the focus tool to make them focus on the field they have missed. what i want is for it to set a variable or something and when that is set the other validation parts do not continue. I hope this makes sence to you 
here is the code i have at current.
thanks guys 
here is the code i have at current.
Code: Select all
<script type="text/javascript">
<!--
function validate_forenames ( )
{
valid = true;
if ( document.part1.forenames.value == "Forenames" )
{
alert ( "Please fill in the your Forenames." );
valid = false;
var box = document.getElementById("forenames");
box.focus();
}
return valid;
}
function validate_surname ( )
{
valid = true;
if ( document.part1.surname.value == "Surname" )
{
alert ( "Please fill in the your Surname." );
valid = false;
var box = document.getElementById("surname");
box.focus();
}
return valid;
}
//-->
</script>
<form method="POST">
<input name="forenames" type="text" id="forenames" onblur="if(this.value=='') this.value='Forenames'; return validate_required ( );" onFocus="if(this.value=='Forenames') this.value='';" value="Forenames">
<br>
<input name="surname" length="40" id="surname" value="Surname" onblur="if(this.value=='') this.value='Surname'; return validate_surname ( );" onFocus="if(this.value=='Surname') this.value='';">
</form>