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

Stay focus and selected when validation fail

Status
Not open for further replies.

khue

Programmer
Mar 6, 2001
112
US
I have the following javascript funciton:

function chkHourlyRate()
{
if (isNaN(document.frmCRA.hourly_rate.value))
{
window.alert("You must enter a valid whole number or decimal number for rate.")
frmCRA.hourly_rate.focus();
frmCRA.hourly_rate.select();
}
}


I have the following 3 text boxes defined inside a form:

$ <INPUT TYPE=&quot;TEXT&quot; NAME=&quot;hourly_rate&quot; MAXLENGTH=7 SIZE=4 onChange=&quot;chkHourlyRate()&quot;> x
<INPUT TYPE=&quot;TEXT&quot; NAME=&quot;total_hours&quot; MAXLENGTH=7 SIZE=4 onBlur=&quot;calculatePay()&quot;> HRS = $
<INPUT TYPE=&quot;TEXT&quot; NAME=&quot;total_pay&quot; MAXLENGTH=7 SIZE=5>


When I enter a invalid number into the hourly_rate text box and press the tab key to go to the next text box, it displays an error but it will not stay focus and selected on this same box, the box that has the invalid number. How can reposition the curser or stop the tab to go forward and refocus on the text box that has the invalid number entered?

I thought that the code above would work but it doesn't. Have some idea?
 
I think that the .select statement is extraneous.

Try taking it out... the .focus() should work fine.

good luck!:)
Paul Prewett
 
khue,

I've only implemented this on intranet sites that are using IE as the supported browser. I don't know if there is a way to use this technique in NS.

In IE you can not cancel the onBlur() event. So if you validate the data in the onBlur() you cannot set the focus to the control that is losing the focus. What you can do is to set a global variable to the control that contains invalid data, then create an onFocus handler in the controls as well. The onFocus handler checks to see if the global variable is NOT NULL, if not it sets the focus to that control then sets the global variable to null.

var g_invalidInputElement = null;

function blurHandler(){
if( data is invalid){
alert(....);
g_invalidInputElelement = window.event.srcElement;
}
}

function focusHandler(){
if (null != g_invalidInputElement){
g_invalidInputElement.focus();
g_invalidInputElement = null;
}
}


Good luck
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top