2 Replies - 12206 Views - Last Post: 07 June 2005 - 11:00 AM

#1 CaolinaN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-June 05

Return focus() after an onBlur() Alert?

Posted 05 June 2005 - 06:11 PM

Although the onsubmit event handler might be 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. ????????

<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>


Thanks for any advise,
Carolina

Is This A Good Question/Topic? 0
  • +

Replies To: Return focus() after an onBlur() Alert?

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Return focus() after an onBlur() Alert?

Posted 05 June 2005 - 07:56 PM

If I were you, I would use the onsubmit handler...barring that, if you must do it field by field, I'd suggest you autopopulate the fileds with some generic message...'Enter Name Here' or something like that, then onblur check to see if the value is null OR the value is 'Enter Name Here'...if it is, you know that the user did not enter anything.

onsubmit is the better way to do this, however.
Was This Post Helpful? 0
  • +
  • -

#3 CaolinaN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-June 05

Re: Return focus() after an onBlur() Alert?

Posted 07 June 2005 - 11:00 AM

Thanks Amadeus. Good suggestion of atopopulating the fields and I will use that. This is just for practice in the book I bought. I agree onsubmit is the better way to do this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1