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

form field focus

Status
Not open for further replies.

joyceda59

Technical User
Apr 3, 2007
29
0
0
CA
Hi,

im just curious to know if there is a way to send the focus to a form field if the user has not filled in that field. Also, i was wondering how to highlight a missing textfield?

I know that in javascript, we can use the Focus() method, but not sure is there is a swimilar method in vbscript. Plz advise! Thx
 
It is practically the same (formfieldelementobject.focus()).
 
oh so..if one of your form field name is packageName, then the syntax is like this:

packageName.focus() ?

thx
 
One format is:
[tt]document.formname.packageName.focus()[/tt]
(provided of course formname has no space and ineligible character.)
 
I tried that:

however i keep getting an object error: "Object Required "

here is what i did:

'Notify users what sections they have left blank
if packageName = "" Then
Response.Write "<br /><center><b>Please enter a <font color ='red'>package name</font></b></center>"
document.NewRequestForm.packageName.focus()

End if

if packageType = "" Then
Response.Write "<br /><center><b>Please select a <font color ='red'>package type</font></b></center>"
document.NewRequestForm.packageType.focus()
End if

if action = "" Then
Response.Write "<br /><center><b>Please select an <font color ='red'> action type</font></b></center>"
document.NewRequestForm.actionRequested.focus()
End if
 
I'm afraid you're mixing serverside and clientside code ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
but how is this related to server side coding. All want to do is set the focus to a particualr field if that field is not filled in by the user. DO you have any suggestion as how how I can do this without mixing server side and client side coding?
 
After server-side validation failed to pass, if you redirect the same page back, you design the same page to write out an onload handler with the focus() etc inside the onload handler. That's how you do it.
 
so this is what i did:

created a sub:

sub CheckForm(form)
'Notify users what sections they have left blank
if packageName = "" Then
Response.Write "<br /><center><b>Please enter a <font color ='red'>package name</font></b></center>"
'document.NewRequestForm.packageName.focus()

End if

if packageType = "" Then
Response.Write "<br /><center><b>Please select a <font color ='red'>package type</font></b></center>"
' document.NewRequestForm.packageType.focus()
End if

if action = "" Then
Response.Write "<br /><center><b>Please select an <font color ='red'> action type</font></b></center>"
' document.NewRequestForm.actionRequested.focus()
End if

if distDate = "" Then
Response.Write "<br /><center><b>Please enter a <font color ='red'>distribution date</font></b></center>"
' document.NewRequestForm.dateTextField.focus()
End if


End sub






i also used the onload handler like this:

<form method="post" name="newRequestForm" action="newPDRItem.asp" onload = "checkForm(this)" >



not too sure if this is correct. Plz advise!
 
[1] I have to say it is not easy. Not because it is difficult, but because there involves many inter-related constructions to make it work. What you show is so much off the essential that I'm not sure you would draw useful elements from what I would outline. The outline too won't tell you some critical design like "which page submit to which page where validation takes place". It seems to suggest the page would submit to itself from what you show. By itself, it is fine. But you have not made clear you have the idea at all what to do when the validation succeeds.

[2] Form element does not support onload handling. You cannot set out an onload handling on it.

[3] One idea is to set up a session variable, called say element_focus. It starts out empty. It is then fed with the first encountered invalidated request.form variable. Once it is filled, further invalidated element won't change it.
[tt]
<%
session("element_focus")=""
'call validation routines...
'...
%>
<html>
<head>
<script language="javascript">
//now script an onload handling
<% if session("element_focus")<>"" then%>
window.onload=function() {
document.newRequestForm.elements["<% =session("element_focus")%>"].focus();
}
<%end if%>
// etc etc...
</script>
</head>
<body>
<!-- etc etc... -->
</body>
</html>
[/tt]
But I think there are hugh gaps to fill in if you are not yet ready to do. And there are so much alternative approaches as well... I would suggest you ask further in the dedicated asp forum.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top