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 event, focus last focus.

Status
Not open for further replies.

samobellows

Programmer
Oct 28, 2009
7
US
Hello all!

here's my basic problem. i have a form validation script that makes sure what the user just entered is a valid entry for that textbox. i have the script running with the onblur event, so that when the user leaves the textbox, it validates the text.

as it stands, the validation is working, but the focus has changed, even if there is an error. what i would like to do is set the focus back to the textbox that the user just left, and select the contents of that textbox, if an error is triggered.

here's a code example:

Code:
var errorID = 0; // global variable.

function formValidation()// this function is called by an onblur event in all of my textboxes
{
   
   validateNumber(); //function sets errorID = 1 if value is not a number
   validateLength(); // there are about 10 different things validated like this, each with a different errorID

if(errorID == 0)...; // runs the script, only if there was no error code

if(errorID == 1) ...; // runs the not a number error message

if(errorID == 2) ...; // and so on for each potential errorID. 

}

so, is there a way to set the focus to the last focus? or maybe cancel the onblur event if an error is triggered? my searches of the forums so far have been enlightening as always, but i haven't found a solution.

Thanks for the help!

Samo
 
Pass the reference of the textbox that you are validating to the formValidation() function. With this reference, you can shift the focus to textbox that you faced the error with. Here is an example:


Code:
  <html>
     <head>
         <script type="text\javascript">
            function formValidation(textBoxBeingValidated) {
               //DO YOUR STUFF
               //ERROR CONDITION?
               textBoxBeingValidated.focus();
            }
         </script>
     </head>
     <body>
         <input type="text" onblur="formValidation(this);" />
     </body>
  </html>

Hope that helps.

Nitin
 
ah, that totally did it. i was passing the entire form to my validation functions, not just the textbox that needed it. a classic thinking outside the box problem. >.<

thanks a ton!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top