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!

How to set focus on ASP to textbox in Javascript on failed validation.

Status
Not open for further replies.

lizok

Programmer
Jan 11, 2001
82
0
0
US
How can I get the focus set on a text box that fails validation ? Here is my invocation of the Validate routine.

Me.txtDUNSNo.Attributes.Add("onChange", "ValidateDUNS(this)")

When bad data is typed in, my error msg shows nicely in the status bar but the .focus and .select methods appear to be ignored. My goal is to trap the error and return the cursor to the bad data with the bad data highlighted. This code works in an ASP environment but fails to put focus and select in .Net.

Hardcoding the element name appears to work as a circumvention but I
am trying for generalized scripts that do not restrict the User
Interface into naming text boxes consistently. All my ValidateXXXX
functions reside on a .JS file in the HEAD of the page:

<HEAD>
<script language=&quot;javascript&quot; src=&quot;../Scripts/Validate.js&quot;></script>
</HEAD>

Thanks in advance to you all. I am using IE6. Here is a sample
function from the .JS page.

function ValidateDUNS(field)
{

var valid = &quot;1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;
var temp
inputStr = field.value
thename = field.name

if ( inputStr.length != 9 && inputStr.length != 13)
{
msgDUNS= &quot;\nDUNS number must be at least 9 or 13 characters long.&quot;
window.status = msgDUNS
field.focus()
field.select()
}
else{window.status=&quot;&quot;
msgDUNS=&quot;&quot; }

}
Post
 
Hey, did you ever figure out a solution to this? I'm having the same issue.

thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top