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!

Javascript Custom Validator Focus

Status
Not open for further replies.

mgreer

Programmer
Jan 9, 2001
45
0
0
US
I'm trying to set the focus and select the contents of a textbox on an aspx page if the javascript Custom Validation fails.. Ex:

<asp:textbox id=&quot;AccountNumber&quot;></asp:textbox>
<asp:CustomValidator id=&quot;custValAccountNumber&quot; Runat=&quot;server&quot; ErrorMessage=&quot;Invalid Account Number&quot; ControlToValidate=&quot;AccountNumber&quot; Display=&quot;Dynamic&quot; ClientValidationFunction=&quot;ValidateAccountNumber&quot;>*</asp:CustomValidator>

<script language=javascript>
function ValidateAccountNumber(oSrc, args)
{
if (args.Value != &quot;123465&quot;)
{
window.alert(&quot;Invalid Account Number!&quot;);
document.getElementById('AccountNumber').focus();
document.getElementById('AccountNumber').select();
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}

For some reason it doesn't seem like the focus or the select is working. The focus just goes to the next field in the tab index. Help please!

 
I hate my workaround for this, but it works like a charm. What I did was I check how my control was renamed in the source of the output html code. I received something like:

conatiner_ctl2_myTextBox

So I changed my javascript to get the elemnt of that ID and it worked.

What is happening? Well, .NET code behind generates scripts (javascripts by default) to perform all the neat .NET stuff. To do that it usually renames your controls. Now, that shouldn't matter because the codebehind should recopgnize what control I am refferring to. It doesn't. That workaround works fine but you need to be careful with it, and it's not GOOD programming either...

hth Daren J. Lahey
Just another computer guy...
FAQ183-874 contains &quot;Suggestions for Getting Quick and Appropriate Answers&quot; to your questions.
Support your forums TODAY!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top