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

OnBlur and Tab Key Problem

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello everyone,
I am new to this website and hope someone can me with this.
I have two input text boxes and want user to enter 3-byte long, if not, alert msg come up.
the problem is when user enter 2-byte and hit TAB key, the alert box is looping.

I appreciate any help
Thanks


<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function ValidateLen(input, x) {
if (input.value.length != x) {
alert('Field must be ' +x+ '-byte in length.');
input.focus();
input.select();
}
}
</script>

<form name=&quot;f1&quot; id=&quot;f1&quot; method=&quot;post&quot; action=&quot;output&quot;>
<FONT FACE=&quot;Verdana&quot; SIZE=2>&nbsp;<strong>Enter Old NPA:</strong>
<INPUT type=&quot;text&quot; name=&quot;field1&quot; size=03 maxlength=03
onBlur=&quot;ValidateLen(this, 3);&quot;
<FONT FACE=&quot;Verdana&quot; SIZE=2>&nbsp;<strong>New NPA:</strong>
<INPUT name=&quot;field2&quot; size=03 maxlength=03
onBlur=&quot;ValidateLen(this, 3);&quot;

<input type=&quot;button&quot; value=&quot;Go!&quot;
onClick=&quot;document.f1.submit();&quot;>
 
why don't you use onsubmit instead of onblur!
if you are interested in using onsubmit, here's the code:
=========================================================
<script language=&quot;javascript&quot;>
function validateForm()
{
if(document.myform.oldNPA.value.length != 3)
{
alert(&quot;OLD NPA must be .....&quot;);
document.myform.oldNPA.focus();
document.myform.oldNPA.select();
return false;
}
if(document.myform.newNPA.value.length != 3)
{
alert(&quot;NEW NPA must be .....&quot;);
document.myform.newNPA.focus();
document.myform.newNPA.select();
return false;
}
return true;
}
</script>

<form name=&quot;myform&quot; action=&quot;&quot; method=&quot;post&quot; onsubmit=&quot;return validateForm()&quot;>
OLD NPA: <input name=&quot;oldNPA&quot; size=&quot;3&quot; maxlength=&quot;3&quot;>
NEW NPA: <input name=&quot;newNPA&quot; size=&quot;3&quot; maxlength=&quot;3&quot;>
<input type=&quot;submit&quot; value=&quot;submit&quot;>
</form>
========================================================= ....:::::.... xxLargeWASP ....:::::....
 
xxLargeWASP,

you have great idea, however I want to alert the user without him/her click on SUBMIT button.
In other word, when the focus is on the first field and he enter 2-byte and hit TAB key, it should show the alert box and when user click OK in the alert box, the focus should be in the first field not SECOND field.


Please advice
Thanks
 
Maybe you could do a check for the ASCII value of the tab key <i>Its okay to dream.....</i>
 
flico,
your code makes infinit loop because you apply the validation function to more than one text field. to avoid the infinit loop do one of these:
1) do the on blur validation for ONE INPUT ONLY.
2) or remove the focus() and select() methods from your code.
3) or - the one i recommend - use the onsubmit validation.

Good luck dude ....:::::.... xxLargeWASP ....:::::....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top