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

document.form.field.focus - not working

Status
Not open for further replies.

NurseIS

MIS
Sep 3, 2001
46
US
After checking for an error in a text field, I want to clear the field and set the focus back to it. Here's a sample so far. I can get the field to null out. Cannot get the cursor back home though with the .focus or .focus() even if I 'rem out the line ...value="" for testing just the .focus line. Have also tried the line with just mainF.usrSSN.focus or mainF.usrSSN.focus()

Sub OnChange_usrSSN()
If IsNumeric(document.mainF.usrSSN.value) = false then
MsgBox "You have entered letter characters in the SSN rather than numbers. Please redo.",,"......Enter numeric characters only"
document.mainF.usrSSN.value = ""
document.mainF.usrSSN.focus
End If
End Sub

Thank you,
 
Hi there

<SCRIPT LANGUAGE=&quot;VBScript&quot;>
Sub Window_onLoad
Document.myForm.pwdPassword.Focus
End Sub
</SCRIPT>

This was example code for setting the focus on a web page, I'm assuming the control is an input textbox?

Transcend
 
Yes, mainF is form. usrSSN is field name. The cursor just stays in the next field in tab order when I use the line like
document.form.field.focus or .focus() or without using the 'document.' at the front end.

Thanks for example and advice.
 
Yes, am running 5.+ Thanks for letting me know about that association. I'm off to see if I can make it work now.

Regards!
 
Ok, I was sure that was all you needed. The actual answer is a little funny (well, so much for my sense of humor) and requires some explanation.

To begin with, the onchange event may not be the best way to do field edits. Are you sure you want field-by-field feedback, or would it be better to edit the whole form in the onclick event for your submit button?

But maybe you want field-by-field. Ok.

The onchange event has the characteristic of being cancelable. The event fires when the form field changes. A change is defined as basically &quot;the user has altered the value and is about to commit the change.&quot; Committing the change means doing something like tabbing to the next field.

So the user types &quot;17322q234&quot; in the SSN field, presses the tab key, and immediately onchange fires.

In your event handler you check the value, become dissatisfied with it, clear the value, and set the focus() back to the SSN field.

But you are already in the SSN field !!!

THEN you exit your event handler without canceling onchange and IE goes ahead and advances focus to the next field.

Ok, you see why I'm laughing? Duh! I knew this, but it was buried someplace in my memory I guess. Sorry I didn't tell you earlier.

Since it is pointless to setfocus() someplace we're already at, we rewrite your event handler so that it is a Function, and return the value
Code:
False
to cancel the change instead of goofing around with focus().

So my test case looks like:
Code:
<html>
<head>
<script language=&quot;VBScript&quot;>
Function usrSSN_onchange()
 If Not IsNumeric(Me.value) Then
   MsgBox &quot;You have entered letter characters in the SSN rather than numbers. Please redo.&quot;,,&quot;......Enter numeric characters only&quot;
   Me.value = &quot;&quot;
   usrSSN_onchange = False
 End If
End Function
Sub window_onload()
  mainF.usrName.focus
End Sub
</script>
</head>
<body>
<form name=&quot;mainF&quot;>
Name <input type=&quot;text&quot; name=&quot;usrName&quot; tabindex=&quot;1&quot;><br>
SSN <input type=&quot;text&quot; name=&quot;usrSSN&quot; tabindex=&quot;2&quot;><br>
Height <input type=&quot;input&quot; name=&quot;usrHeight&quot; tabindex=&quot;3&quot;>
</form>
</body>
</html>
And it works just fine... or does it???

Well, if a user loads this page, types in &quot;Tom&quot; for his name, tabs to the SSN field and types &quot;a&quot; for his SSN, and then tabs again...

Your message pops up, and after OKing the notice the SSN field is cleared, and the cursor is left at SSN for another try. Looks good.

But if he then tabs again without typing a new SSN value, the cursor blithely advances to the Height field in this example!

Well, duh! Nothing changed!

So basically using onchange, while seeming cool on the surface, sorta sucks for field value validation. This is also one reason I tend to validate just prior to permitting a submit to proceed.

But if you really want field-by-field checks, then go for it - there's nothing wrong with it in principal.

I suggest you replace your onchange event handler with an onblur handler instead however, such as:
Code:
Sub usrSSN_onblur()
 If Not IsNumeric(Me.value) Then
   MsgBox &quot;You have entered letter characters in the SSN rather than numbers. Please redo.&quot;,,&quot;......Enter numeric characters only&quot;
   Me.value = &quot;&quot;
   Me.focus
 End If
End Sub
Note that onblur cannot be canceled. But by definition it is fired after the item (field) loses focus, so we can use our good buddy the focus() method this time. And your validation logic will be invoked every time the user enters and then leaves the field.

Crisis averted, world saved.

By the way, did you notice I replaced the bulky
Code:
document.mainF.usrSSN
references with the VBScript
Code:
Me
instead? Cool, isn't it?

Oh, and you could get away with just
Code:
mainF.usrSSN
if you wanted to as well. The reason just saying something like
Code:
usrSSN.value
doesn't work is that most types of HTML items, when they occur in a <form>, are placed into the form's namespace rather than the document's namespace. Thus in script they need to be qualified by the form name except in script blocks that also occur within the same <form>.


Hopefully this was useful and your sense of humor is intact. Good luck!
 
I had reworked the overall form verification rather than checking each field. Then adjusted with what you've suggested. It works! Thank you very much for the extra time and effort to work this out! I am not at home right now. Will past in an example later.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top