PDA

View Full Version : Return focus() after an onBlur() Alert???


CarolinaN
06-06-2005, 12:29 AM
Although the onsubmit event handler might sound like my best choice, I am trying to figure out how to to check in for individual fields to make sure data was entered. Here is the codeI am using. It works, but the problem is that I have to have values already in at least one of the fields or it will go into an unbreakale loop (CTRL-ALT-DEL) if you try to leave either field without something in the other one. Notice how if you delete the field and tab out that it alerts you an entry is required and focuses back on that field. Now how to do it with both field blank on startup. ????????

This is what I have:

<html>
<head>
<title></title>
<script language="JavaScript">
function validate_name(fobur) {
if ( fobur.user_name.value.length <1 ){
alert("You must enter your name.");
fobur.user_name.focus();
return false;
}
else {
return true;
}
}
function validate_address(fobur) {
if ( fobur.user_address.value.length <1 ){
alert("You must enter your address.");
fobur.user_address.focus();
return false;
}
else {
return true;
}
}
</script>
</head>
<body>
<h2> Checking for Empty fields </h2>
<form name="fobur">

Please enter your name: <br>
<input type="text"
size=50
name="user_name"
value="name:"
onBlur="return validate_name(fobur)">
<p>


Please enter your address: <BR>
<input type="text"
size=30
name="user_address"
value ="address:"
onBlur="return validate_address(fobur)">
<p>

<input type=submit value="Send">
<input type=reset value="Reset">
</form>
</html>