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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

An onChange event handler doesn’ t work in Netscape 7.2 1

Status
Not open for further replies.

ericnet

Programmer
Mar 29, 2006
106
I have a function in my page that is triggered with an ‘onChange’ event handler from a tex box. If the user enters ‘0’ in the text box, when she/he wants to move to another element in the page an alert appears, and the cursor is focused again in the text box so that is modified. In IE works fine, but in Netscape 7.2 it doesn’ t, the alert appears only the first time the user moves to another element, after that the cursor isn’t focused in the text box and the alert doesn’ t appear anymore.

Here is the code sample:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
<!--//

function set_cursor() {
 document.my_form.caliber_min.focus()
}

function check_field25() {

  var cal_min = document.my_form.caliber_min.value
  
  if (cal_min == "0") {
     alert("You must enter a value greater than 0.")
     document.my_form.caliber_min.focus()
     return false; 
   }  	

 return true;
}

// -->
</script>

</head>
<body onLoad="set_cursor()">
<form name="my_form">

<input name="caliber_min" type="text" maxlength="8" id="caliber_min" class="letter23" [b]onChange="return check_field25(this);"[/b] style="width: 50" /><br>
<input type="text" width="100">

</form>
</body>
</html>

How can I solve this problem in Netscape 7.2?

Thanks
 
Maybe it's a script error. Do you see anything in the JS console?

Tools -> Web Development -> JavaScript console

Perhaps you could try ending more of your statements with semicolons (you should always do this in JS, really)... maybe NN is getting confused about what lines are what?

Dan




[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi,
in my NN7.2 it does what I explained with the exact code sample I posted. I don't know what can happen..
 
after you entered '0' value in the text field (in NN7.2), and after accept the alert, can you move to the other text form element? If so, this is what I mean. In IE you can't move to another form element if the '0' isn' t modified
 
now I have to go, I'm going to check the post later

thank you
 
Dan. Would you mind posting the exact code that worked for you please?

 
Then, if you are testing exactly the same code in NN7.2, the only explanation is you don’ t see the different behavior the OnChange event handler has in IE and NN.

In IE you can’ t move mouse cursor to another form element until you change the condition that makes the function focus on the element and return false, so, you must change ‘0’ value if you want to leave the text field. Instead, in NN you can move cursor to another form element without changing the ‘wrong’ value. So, in NN the onChange event handler only is fired the first time you want to leave the text field with the ‘wrong’ value on it, after that (if you don’ t change the value) the onChange event isn’t fired anymore.

I tried onBlur event instead, and this works in the same way in both browsers. The only thing now it doesn’ t work in NN is ‘focus()’ method..

 
[1] Use onblur instead of onchange. This explain why the alert won't appear the 2nd time.
[2] Give a time delay for the re-focusing.
[tt]
function check_field25([blue]obj[/blue]) { //why neglect the argument in your script?

var cal_min = [blue]obj[/blue].value

if (cal_min == "0") {
alert("You must enter a value greater than 0.")
[red]setTimeout(function() {obj.focus();},50}[/red];
[green]//[/green]return false; //ambiguous in purpose---no need
}

[green]//[/green]return true; //ambiguous in purpose---no need
}

<input name="caliber_min" type="text" maxlength="8" id="caliber_min" class="letter23" on[blue]blur[/blue]=[highlight]"c[/highlight]heck_field25(this);" style="width: 50" />
[/tt]
 
Yes! this solves the problem of onBlur event in Netscape, and now I can validate my text fields just after the user has filled them, thank you very much :).

I don' t understand why Netscape hasn' t solved this, since 'onChange' and 'onBlur' events are a very common way to validate web forms in js..
 
Thanks for the vote! But it seems you have to note one thing to get the idea. It is not Netscape hasn' t solved any matter related to onblur vs onchange---saying so would upset a lot of their fans. What I meant to use onblur is because when "0" is already in the text box and refocus is successfully done, and you refuse to change the "0" and tab to the next field, the text box would then not trigger an onchange event, because, nothing has been changed. That's all what I meant.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top