Using .NET Validation Controls

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

I know how to use these great controls. They work very
well. But is there a method or property I can use to set
the focus to the field that is in error.

For example... If I have a page with 5 text boxes on it
and they are all reaquired fields. When my user falis to
enter in something in field 3 and presses the Submit
button, the Summarty validation control fires just fine
but the focus is still on the Submit Button.

Is there a way to utilize the RequiredFieldValidator or
SummaryValidationControl fields to set the focus to
txtField3?

Thanks

Tom
 
Set the focus manually to the control yourself... like this:

// if userID (a TextBox) is not entered, or is invalid
// set the focus to it.
userID.Select();

Hope that helps,
-JG
 
The problem is where? The submit button event does not
fire. Its like the Validation controls are a pseudo
JavaScript, only without the bells and whistles.

I know how to set focus, I just don't know where to put
the code since the Submit Button's click event does not
fire.
Tom
 
The problem is where? The submit button event does not
fire. Its like the Validation controls are a pseudo
JavaScript, only without the bells and whistles.

Oh, my. I'm so sorry, I thought this was a question about validators and
controls on Winforms. My mistake.

Well, I looked around the docs on ASP.Net's validators and found no way of
doing it. You can do it with some javascript of your own, though (sample
..aspx page below). Just handle the onsubmit event of the form yourself
(ASP.Net will modify it so it calls it's validation routine first) and check
if your validators have their style.display properties set to "none". If
there is an error, this property will be moething other than "none" and you
then set the focus to the control.

Hope that helps,
-JG

<!-- Test.aspx -->

<%@ Page language="c#"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="FlowLayout">
<script language="javascript">

function SetFocusOnError(theForm) {
if (emailValidator.style.display != "none" ||
validEmailValidator.style.display != "none") {
theForm.email.focus();
theForm.email.select();
return;
}
if (ageValidator.style.display != "none" ||
overThirteenValidator.style.display != "none") {
theForm.age.focus();
theForm.age.select();
return;
}
}

</script>

<form id="Test" method="post" runat="server"
onsubmit="SetFocusOnError(this);">

<P>
Email: <asp:TextBox id="email" runat="server"></asp:TextBox>

<asp:RequiredFieldValidator id="emailValidator" runat="server"
ErrorMessage="Enter your email." Display="Dynamic"
ControlToValidate="email"></asp:RequiredFieldValidator>&nbsp;

<asp:RegularExpressionValidator id="validEmailValidator" runat="server"
ErrorMessage="Enter a valid email address." Display="Dynamic"
ControlToValidate="email"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator> <BR>

Age:
<asp:TextBox id="age" runat="server"></asp:TextBox>

<asp:RequiredFieldValidator id="ageValidator" runat="server"
ErrorMessage="Enter your age." Display="Dynamic"
ControlToValidate="age"></asp:RequiredFieldValidator>&nbsp;

<asp:CompareValidator id="overThirteenValidator" runat="server"
ErrorMessage="Must be older than 13." Display="Dynamic"
ControlToValidate="age" Type="Integer" ValueToCompare="13"
Operator="GreaterThan"></asp:CompareValidator><BR>

<asp:Button id="Button1" runat="server" Text="Submit the form"
EnableViewState="False" OnClick="RefreshMe"></asp:Button></P>

<p><asp:Label id="lblMsg" runat="server"></asp:Label></p>
</form>
</body>
</HTML>

<script runat="server">
public void RefreshMe(object sender, EventArgs e) {
lblMsg.Text = "Submit accepted at: " +
DateTime.Now.ToLongTimeString();
}
</script>
 
Hello,

I think you should use the CustomValidator control, that control gives
you
the option to write your own Client-side and/or server side
validationcode.

Example:
You could write a generic javascript function which checks a field
for....whatever.

<script language="javascript">
function ValidateField(source,arguments)
{

if(#do checks#)
{
arguments.isValid = false; // Sets the Page.IsValid property
to true
source.focus();
}
else
{
arguments.isValid = true;
}
}
</script>

- add a CustomValidator control to your form and set the following
properties:
- ControlToValidate = ControlToValidate (what a suprise)
- ClientValidationFunction = ValidateField


you should also write a serverside validation function in case the
users browser doesn't support javascript for some reason.

I haven't tried this myself, but i think it should work. I read in the
MSDN documentation this doesn't work for manditory checks because the
validation function doesn't fire when te control is empty, but you
could try it.

Good luck,

Eelco Duinsbergen
 
Back
Top