Solved

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

Posted on 2004-09-06
3
3,077 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
 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

728 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