Solved

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

Posted on 2004-09-06
3
3,044 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
3 Comments
 
LVL 9

Expert Comment

by:glsac
Comment Utility
0
 

Author Comment

by:tidydave
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
popup message 3 26
401 - Error 6 24
Problem to picture file 3 37
Trouble with References... 5 22
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now