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

onblur problem when attempting to set focus

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
I have a simple form with a number of text fields, check boxes and radius buttons.

The text fields need to be validated for data type format (phone, date, time, etc.). I use the onblur behavior to check entered value, validate and proceed accordingly.

Now, image fields PHONE1 and PHONE2, they are both next to once another. the onblur calls for a script which in turn makes an AJAX call to a PHP script. The script checks and validates data and returns a response ... All of this is working except that when the data is not valid, the script sets focus to the field based on its ID but some how, the same script is called by the onblur of the 2nd field as the first regains focus.

In other words, user presses TAB from PHONE1 - routines validates, fails and set focus to PHONE1. The browser, naturally attempts to place cursor in PHONE2 and it appears that the .focus() command triggers the same routine but for PHONE2.

I would not have a problem if not because I am using a "shared" layer where to display message returned by PHP script with a detailed explanation of why data may have failed.

How do I skip the onblur action for PHONE2 if PHONE1 validation had failed?


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
either you have to debug when the script fires. or let it fire but conditionalise the execution of the main part of the script by some variable that you set/unset depending on whether you are in the process of validating another field etc.

or you can use the jquery .off and .on methods to remove the events.
 
I think the question is how you are setting the focus back to Phone1 when it fails,

Because it sounds like Phone2 is getting focus, and immediately loosing it, triggering the blurr event, and thus the function. Perhaps instead of returning focus to the previous field, simply highlighting it with a colored border or something similar would be a better choice. Thus avoiding the focus-blurr cycle that can be very hard to get out off.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top