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!

Why would it be that when I call a 2

Status
Not open for further replies.

KizMar

Technical User
Mar 13, 2002
52
US
Why would it be that when I call a function:

"
function validateZip(textObj)
{ // Must be all numbers and 5 digits
var str = textObj.value
var aChar = ""
var formObj = document.frmAbstractor

if (str.length != 5) {
alert('Please enter a valid Zip Code.');
document.frmAbstractor.txtZip.focus();
document.frmAbstractor.txtZip.select();
return;
}
for (var i=0; i < str.length; i++) {
aChar = str.charAt(i);

if (isNaN(aChar)) {
alert('Zip Code must be numbers only.');
document.frmAbstractor.txtZip.select();
return;
}
}
return;
}
&quot;

... with the following code:

&quot;<input type=&quot;text&quot; name=&quot;txtZip&quot; id=&quot;txtZip&quot; value=&quot;<%=varZip%>&quot; maxlength=&quot;5&quot; size=&quot;5&quot; onChange=&quot;validateZip(this)&quot; class=&quot;RequiredField&quot;>&quot;

When it's supposed to throw the alert and set focus back to the txtZip field, it's still setting focus on the next field in the form??? As you can see I've tried just Focus() and Select() and both... nothing...
 
KizMar,

Easier to do that sort of thing with regular expressions
now.. Take a look at this see if it will work out for you.

Code:
<html><head><script>
function validateZip(textObj)
{ // Must be all numbers and 5 digits
var str = textObj.value
rslt = /^[0-9]{5}$/.test(str);
if(!rslt) {
alert('Zip Code must be &quot;5&quot; digits.');
textObj.focus();
textObj.select();
return false;
}
else {
alert(&quot;Yeah 5 digits&quot;);
return true; 
}
}
</script></head><body><form name=&quot;frmAbstractor&quot;>
<input type=&quot;text&quot; name=&quot;txtZip&quot; id=&quot;zipp&quot; size=&quot;5&quot; onBlur=&quot;validateZip(this)&quot;>
</form></body></html>

2b||!2b
 
Well, that does work with less code, thank you for showing me a quicker way to check that! Unfortunately though, I still can't get it to set the focus &/or select the text in the field. It's still moving the cursor to the next field in the form. =(
 
KizMar,

As of IE 5, any objects that need to have focus set must have a TABINDEX attribute set.

You also need to modify the code slightly:

Code:
function validateZip(textObj)
{
	// Must be all numbers and 5 digits
	var str = textObj.value;
	var aChar = &quot;&quot;;
	var formObj = document.frmAbstractor;

	if (str.length != 5)
	{
		alert('Please enter a valid Zip Code.');
		formObj.txtZip.focus();
		return(false);
	}
		else
	for (var i=0; i < str.length; i++)
	{
		aChar = str.charAt(i);
		if (isNaN(aChar))
		{
			alert('Zip Code must be numbers only.');
			formObj.txtZip.focus();
			return(false);
		}
	}
	return(true);
}

...

<input type=&quot;text&quot; name=&quot;txtZip&quot; id=&quot;zipp&quot; size=&quot;5&quot; onBlur=&quot;return(validateZip(this));&quot; tabindex=&quot;1&quot;>

This seems to work for me in IE, although it still fails occasionally in NN ;o(

Hope this helps!

Dan
 
Thanks Dan for the detail.

KizMar,

If you still have trouble maybe strip your code down to
the bare minimum and post form and all, so we can take
a look at your form elements.

2b||!2b
 
That worked, it was the &quot;return&quot; that I needed in the input tag. WOHOOO!!! Thanks!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top