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

Netscape Javascript

Status
Not open for further replies.

whiskey

Programmer
Sep 15, 2002
4
US

Hi

I have a text field, and i need to validate it.
I want to return focus back to it if the value entered
is not numeric. I do this by capturing the onblur event
and calling the validate function.
It works well on IE but for some reason doesnot work
on Netscape 6.2.1
Any suggestions.

====
<HTML>
<HEAD>
<TITLE>Validate</TITLE>
<script language=&quot;JavaScript&quot;>
function validate()
{
var val = document.testForm.txt1.value;
if(val == 1) {
return true;
}
alert('dont submit');
return false;
}
function validatetxt1()
{
var val = document.testForm.txt1.value;
if(val == 1) {
return true;
}
alert('invalid values');
return false;
}
</script>
</HEAD>

<BODY>
<FORM METHOD=POST ACTION=&quot;result.html&quot; NAME =&quot;testForm&quot; >
<INPUT TYPE=&quot;text&quot; NAME=&quot;txt1&quot; onChange=&quot;return validatetxt1();&quot;>
<INPUT TYPE=&quot;text&quot; NAME=&quot;txt2&quot;
onBlur = &quot;if(txt2.value=='2'){
alert('onblur');return true;
}else{
alert('returning false BLUR');return false;
}&quot;>
<INPUT TYPE=&quot;text&quot; NAME=&quot;txt3&quot; >
<input type=&quot;submit&quot; onClick=&quot;return validate();&quot;>
</FORM>
</BODY>
</HTML>

====
Somehow the onchange/onblur function does not return false

 
Everything works correctly in my Mozilla.
One thing: instead of using submit onclick you should use form onsubmit:

<form ... onSubmit=&quot;return validate()&quot;>
 

Hi,

I guess i didnt make my question clear.
Here is what I want to do.

I have three text fields. Whenever i remove focus
from the second txtfld, i check the onblur event,
to see if the value is 2. If not then i want to retain
focus on textfield2. But this does not seem to work.
I do get the alert message, but somehow i am unable to capture the &quot;return false&quot;, which i guess would prevent
textfield2 from losing focus. I even tried to add textfield.focus() after the alert, but then it goes in an infinite loop.

Same thing for textfield1, where i check the onchange event.

I hope i made my point clear.

 
Put something like this between your alert notice and your return (false) statement:

document.form.<fieldname>.focus();

This will put the focus back on the field. There's always a better way...
 

Hi

I have tried even that, but it doesnt work in Netscape6.2
Please note, that everything works fine in IE.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top