Solved

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

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Angular JS Route 3 70
ASP.net Have sliders change in order 2 23
EF5: Question about Metadata Artifact Processing 4 15
asp enable view state 1 24
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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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