Faqts : Computers : Programming : Languages : JavaScript : Event handling

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 15 people (47%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How to prevent focus to location bar when I want the focus to be on the field after validation?

Jul 20th, 2001 08:42
A A, Jean-Bernard Valentaten,


Here is wht I am doing :
<INPUT TYPE=Text NAME=p_vdteFrom CLASS=classTDL SIZE=10 VALUE="" 
MAXLENGTH= 10 onChange="return(isValidDateFrom(this))>
Here are the two function being callled :
    function isEmpty(inputStr) {
        if (inputStr == null || inputStr == "") {
            return true;
        }
        return false;
    }
    function isValidDateFrom(field) {
        var inputVal = field.value;
        if (isEmpty(inputVal)) {
            alert("Date From cannot be empty.");
            field.focus();
            field.select();
            return false;
        }
        return true;            
    }        
Everything looks fine to me but when the validation fails instead of 
the focus returning to the field, the focus goes to the location bar.
Any clues ??
=======================================================================
As far as I could read out of your question-changes (*g*) what you have 
ist the following:
<input type="text" onChange="validate(this.value);">
and a function called validate(strToCheck) or something similar.
So after the validation process you want the focus to be back on the 
input-field.
The best possibility is to add a variable to your function, that 
contains the field-object. This would smoehow look like this:
<input type="text" onChange="validate(this.value, this);">
where the function would be declared like this:
function validate(strToCheck, callingElement)
{
  //here you do the validation stuff
  if (isFalse)
    callingElement.focus();
}
somewhat like that.
HTH
Jean