?
Solved

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

Posted on 2004-09-06
3
Medium Priority
?
3,081 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 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