Forum Moderators: open

Message Too Old, No Replies

Validation to control textbox properties

         

IntegrityWebDev

2:37 pm on Mar 4, 2010 (gmt 0)

10+ Year Member



I have another validation question:
I have a page with a LOT of form fields and I'm validating them (decided to go with the .NET form instead of AJAX...for now).

All of the fields and validators follow this format:

<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ForeColor="" InitialValue="" ControlToValidate="txtEmail" ID="txtEmail1" runat="server" ErrorMessage="Required Field" Display="Dynamic" /></div>


So that whatever the name of the field is, the name of the validator is that name with a number "1" on the end.

Given that is there any kind of page-wide function I can do that says if any field doesn't validate make that field background color turn to color "#FFO"?

Thanks!
Chris

marcel

3:37 pm on Mar 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would recommend jQuery for this, but as you want to do this in .Net I thought I would throw this example together:

HTML
<asp:TextBox ID="txtEmail" runat="server" />
<asp:RequiredFieldValidator EnableClientScript="false" ID="rfvEmail" runat="server" ControlToValidate="txtEmail" ErrorMessage="*" />
<br />
<asp:TextBox ID="txtOther" runat="server" />
<asp:RequiredFieldValidator EnableClientScript="false" ID="rfvOther" runat="server" ControlToValidate="txtOther" ErrorMessage="*" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />


Code Behind:
protected void Button1_Click(object sender, EventArgs e)
{
// First reset the styling of all TextBox controls
foreach (Control c in form1.Controls)
{
if (c is TextBox)
{
TextBox tb = (TextBox)c;
tb.Attributes.Clear();
}
}

// Now check for invalid TextBoxes
if (!Page.IsValid )
{
foreach (Control c in form1.Controls)
{
if (c is RequiredFieldValidator)
{
RequiredFieldValidator rfv = (RequiredFieldValidator)c;
if (!rfv.IsValid)
{
// Try to find the matching TextBox control (the ControlToValidate)
TextBox tb = (TextBox)form1.FindControl(rfv.ControlToValidate);
if (tb != null)
{
tb.Attributes.Add("style", "background-color:#ff0");
}
}
}
}
}
}


Naming convention is not important, as long as the RequiredFieldValidator has ControlToValidate property filled.

* It's important to set EnableClientScript="false" to disable client side validation.

marcel

7:57 am on Mar 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I forgot to add that although I do prefer client side validation for this kind of thing, the above code will always work, even when the browser has JavaScript disabled.

IntegrityWebDev

1:15 pm on Mar 5, 2010 (gmt 0)

10+ Year Member



Thank you once again Marcel...I will study your code and work with it.

marcel

7:48 am on Mar 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A little late, but here is a great article on Using Single Validator for Multiple Controls [codeproject.com]

IntegrityWebDev

1:31 pm on Mar 9, 2010 (gmt 0)

10+ Year Member



Thanks Marcel...I will read the article for future reference. Working on this project, I've learned a lot about validators and ASP.NET in general!