Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

focus() problem 1

Status
Not open for further replies.

PatrickIRL

Programmer
Jun 25, 2003
383
0
0
Hi,

I have an HTML page with some fields that require validation. What I am doing is on the onblur() event I'm validating the field. If there is invalid data I want the focus to stay on that field. This is working fine in IE but not Netscape or Mozilla. As a test I produced the following, can anybody shed some light on the subject?

<html>
<head>
<script language=&quot;javascript&quot;>
function validateDobDay(){
if(document.getElementById(&quot;dobDD&quot;).value < 10){
alert(&quot;Please enter a value greater than 10.&quot;);
document.getElementById(&quot;dobDD&quot;).value=&quot;&quot;;
document.getElementById(&quot;dobDD&quot;).focus();
return false;
}
}
</script>
</head>
<body>
<input type=&quot;text&quot; id=&quot;dobDD&quot; size=&quot;2&quot; onblur=&quot;validateDobDay()&quot;>
<input type=&quot;text&quot; id=&quot;dobMM&quot; size=&quot;2&quot;>
</body>
</html>

Thanx in advance ....

:)
 
I started using &quot;this.form&quot; cuts down
on all that &quot;document.&quot; notation.

It passes the form as the object automatically.

And the regular expression /^\d{2}$/ checks that
the user entered 2 digits.

Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<html><head><title>TEST Page</title>
<script language=&quot;JavaScript&quot;>
  function validateDobDay(frm){
    if(!frm.dobDD.value.match(/^\d{2}$/) ||
	     frm.dobDD.value > 31){
      alert(&quot;Please enter 2 Digits e.g. 05 O.K.&quot;);
      frm.dobDD.value=&quot;&quot;;
      frm.dobDD.focus();
      return false;    }
   return true;  }
</script></head><body>
<form>
Day<br>
<input type=&quot;text&quot; id=&quot;dobDD&quot; size=&quot;2&quot; onblur=&quot;validateDobDay(this.form)&quot;>
<br>Month<br>
<input type=&quot;text&quot; id=&quot;dobMM&quot; size=&quot;2&quot;><br>
</form></body></html>

2b||!2b
 
Nope, tried them all. The focus still goes onto the next input, any ideas ???

 
I am thinking to make it work like you want
you'll have to use a different event method
like the button in this form.

You can incorporate the form submission into
the validation script.

This reference may be helpful:

Tested this page in NN7 and IE6
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<html><head><title>TEST Page</title>
<script language=&quot;JavaScript&quot;>
  function validateDobDay(frm){
    if(!frm.dobDD.value.match(/^\d{2}$/) ||
         frm.dobDD.value > 31){
      alert(&quot;Please enter 2 Digits&quot;);
      frm.dobDD.value=&quot;&quot;;
      frm.dobDD.focus();
      return false;    }
   alert(&quot;Form Submitted Here&quot;);
   //document.test.submit();
   return true;  }
</script></head><body>
<form name=&quot;test&quot;>
Day<br>
<input type=&quot;text&quot; id=&quot;dobDD&quot; size=&quot;2&quot;>
<br>Month<br>
<input type=&quot;text&quot; id=&quot;dobMM&quot; size=&quot;2&quot;><br>
<input type=&quot;button&quot; value=&quot;Submit&quot; onClick=&quot;validateDobDay(this.form);&quot;>
</form></body></html>

2b||!2b
 
This is interesting because I had this exact same problem yesterday :)

In IE you can stop some events by returning false in their event handler (eg onsubmit=&quot;return false;&quot;).
Returning false with onblur doesn't work, but you CAN focus on the element again, which has pretty much the same effect (onblur=&quot;this.focus();&quot;).
However mozilla's whole event system is totally different, and basically the above doesn't work. I would welcome a solution..

Here's a partial work around: delay the focus() by a millisecond:
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd&quot;>[/URL]
<html>
<head>
<title>testbed</title>
<style type=&quot;text/css&quot;>
</style>
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
function check(txt) {
	if (txt.value == &quot;&quot;) {
		alert(&quot;please enter a value&quot;);
		setTimeout(&quot;document.getElementById('text1').focus()&quot;, 1);
	}
}
</script>
</head>
<body>
<form>
<input type=&quot;text&quot; id=&quot;text1&quot; size=&quot;30&quot; onblur=&quot;return check(this);&quot; />
<input type=&quot;text&quot; id=&quot;text2&quot; size=&quot;30&quot; onblur=&quot;return false;&quot; />
</form>
</body>
</html>

Still I couldn't find a decent page explaining mozilla's event system (specifically how to cancel one).

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
clarkin,

I didn't even notice that the page I posted was using
&quot;id=&quot; instead of &quot;name=&quot; for the form fields I just cut
and paste from the original post. I would and should
have changed that.

What if any is the advantage of using id for a form field
instead of name??

mark,

2b||!2b
 
Not really much difference: I just used id= so I could refer to them using the DOM with document.getElementById().

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Hi all, many thanks for your replies and suggestions. The only reason I have to do this is because I had originally used VBScript for validation (I was told by the client that ALL his people use IE). However :-( , turns out that ONE of his client uses AOL/Netscape exclusively. I want the same functionality if possible using JavaScript, I'll keep trying, again thanks for the input, much appreciated.

Patrick
 
Just a note, searched mozilla.org, found nothing that would help me to fix this problem. :-( There are plenty of examples using onblur() and focus(), yet no issues. Curious, am I doing something WAY wrong ???????

Patrick
 
Try out killing the event by inserting the following in your block before the ... .focus();

window.event.returnValue=false;
window.event.keyCode=0;

If this is wrong I would continue on this idea - STOP THE EVENT.

Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Thanx theboyhope, thought it was just me, ah well

clarkin, using the setTimeout method, let you know what happens,

:)
 
YES, YES, YES, OH YES !!!!!!!!!!!!

Thanks clarkin, setTimeOut works, take a star, my treat.

Patrick
 
Sounds like you should do all your client-side scripting in javascript - It works just fine in IE too.

[spin2]

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top