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() in Firefox

Status
Not open for further replies.

RISTMO

Programmer
Nov 16, 2001
1,259
0
0
US
Hey,

I have this code that works fine in IE, but it totally chokes in Firefox.

Type in an invalid phone number in the phone number field, then tab to the next field, and you'll see the alert popup, but the field doesn't focus again requiring them to leave it empty or valid, like it should. Here's the code. Any ideas?

Code:
function ValidateUSPhone (theField)
{
	var msg = "";
	var theString = "";
	var msgInvalid = "Please enter a valid U.S. phone number.\nSuch as (760) 555-1212"

	theString = theField.value;

	if (theString != "")
	{
		theString = StripChars(theString)		

		if (!AllInRange("0","9",theString))
		{
			msg = msgInvalid;
		}
		else if (theString.length == 11)
		{
			 if (theString.charAt(0) != "1")	
				msg = msgInvalid
		}
		else if (theString.length != 10 && theString.length != 7)
		{
			msg = msgInvalid
		}

		if (msg == "")
		{
			if (theString.length == 10)
				theString = reformat(theString,"(",3,") ",3,"-",4);
			else if (theString.length == 7)
				theString = reformat(theString,"",3,"-",4);
			else //len == 11
				theString = reformat(theString,"",1,"(",3,") ",3,"-",4);

			theField.value = theString;
		}
		else
		{
			alert(msg);
			theField.focus();
		}
	}
}

Thanks,
Rick

RISTMO Designs: Rockwall Web Design
Arab Church: Arabic Christian Resources
Genuine Autos: Kaufman Chevrolet & Pontiac Dealer
Rick Morgan's Official Website
 
That is kind of quirky. I went to your page and if I minimized the page and then came back to it the focus was on the input field. This won't solve your exact problem because you are passing a an object into your ValidatePhone function but just test using ID's to get your object. Try the following to see if it works. Give your form the id "HomePhone" then try this:
Code:
document.getElementById("HomePhone").focus();
 
Hey,

Thanks for the idea. I changed it to onblur="document.getElementById('HomePhone').focus();" just to test, and once again it worked great in IE but not at all in Firefox. It seems like this is a problem with focus(), not referencing the field... it seems like Firefox focuses the field first and then continues to move to the next field.

Any other ideas?

Rick

RISTMO Designs: Rockwall Web Design
Arab Church: Arabic Christian Resources
Genuine Autos: Kaufman Chevrolet & Pontiac Dealer
Rick Morgan's Official Website
 
i seem to remember something about the tabindex attribute needs to be set when using focus()? try that, also try reversing the order from:
else
{
alert(msg);
theField.focus();
}

to
else
{
theField.focus();
alert(msg);
}

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top