Forum Moderators: open

Message Too Old, No Replies

Control TextBox Border with Validation

         

IntegrityWebDev

2:06 pm on Mar 1, 2010 (gmt 0)

10+ Year Member



Recently this list helped me a lot with understanding form validation in ASP.NET (C#)...thanks!

Now I'm back with another question. I understand the basics of validation...but I'm wondering, is there a way to use form validation to control the border color (and other attributes) of a TextBox.

Here's my code:

<asp:TextBox ID="name" runat="server" ForeColor="#2378B8" Width="245px"></asp:TextBox>

<asp:RequiredFieldValidator InitialValue="" ControlToValidate="name" Display="Dynamic" ID="name1" runat="server" ErrorMessage="<br>Required Field." />


Bonus question...can this also be done for drop down lists?

Thanks!
Chris

marcel

2:43 pm on Mar 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is a very simple example with a text box and a Custom Validator, this will also work with a DropDown List:

HTML
<asp:TextBox ID="TextBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="*" onservervalidate="CustomValidator1_ServerValidate" />
<asp:Button ID="Button1" runat="server" Text="Button" />


Code behind:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (TextBox1.Text == String.Empty)
{
TextBox1.Style.Add("border", "solid 1px red");
args.IsValid = false;
}
}


A more elegant solution would be to use something like jQuery, a search for jQuery Validation will yield a multitude of possible solutions.

IntegrityWebDev

3:10 pm on Mar 1, 2010 (gmt 0)

10+ Year Member



Marcel...you come through again! Thanks I will check out this solution and also search for jQuery Validation.

marcel

3:23 pm on Mar 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I forgot to add a link to the Javascript/Ajax forums, they'll be able to help you out with jQuery there:
[webmasterworld.com...]