Solved

ASP.NET Form Validation Javascript - Set Focus to required field

Posted on 2004-09-06
3
3,073 Views
Last Modified: 2011-09-20
hi,

i have had a look on google about this but couldn't find anything.

the situation is this... i have a large form with a number of required field validators on it.  there's a button at the bottom of form.  when it's clicked the form validation fires.  i've set up a validation summary to pop up an alert box containing the fields that require information.

however, what would be even better, would be to set the focus to the first field in the list, that requires information.  so the process would go:

1.  click button.
2.  validation summary show a pop up with a list of fields
     that require info.
3.  some extra javascript written by you guys, would set the
     focus to the first field in the pop up.

kind regards,
dave.


0
Comment
Question by:tidydave
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Expert Comment

by:glsac
ID: 11989491
0
 

Author Comment

by:tidydave
ID: 11990021
thanks for the javascript.  however we have done it this way before.  what we want to do now, is add some javascript, to interact with the ASP.NET required field valiators AND/OR validation summary.  so that the element that is first in the validation summary list has focus.  it is an improvement on the current functionality.

cheers, dave :-)
0
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 500 total points
ID: 11990922
tidydave,
This is how i solved the problem exactly based on your need.
eg:
<script>
function CheckRequired()
{
      for(var i=0; i< Page_Validators.length; i++ ) {

                  ValidatorValidate(Page_Validators[i]);

                  if (!Page_Validators[i].isvalid) {
                        t = eval("document.all." + Page_Validators[i].controltovalidate);
                        t.focus();
                        t.select();
                        break;

                  }

         }
}
</script>

<form id="Form1" method="post" runat="server" onsubmit="CheckRequired();">
<asp:textbox id="txtName" Runat="server"></asp:textbox><asp:requiredfieldvalidator id="valTest" Runat="server" ControlToValidate="txtName" ErrorMessage="ERROR"></asp:requiredfieldvalidator><br>
<br>
<asp:validationsummary id="valSumError" Runat="server"
ShowMessageBox="True"></asp:validationsummary>
</form>                  

-More details, you can check with this site :
http://www.cmap-online.org/Default.aspx?tabindex=8&tabid=-5&ItemID=-10

Regards
x_com
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question