Solved

How to change the textbox background color when the Regular expression shows an error?

Posted on 2009-04-12
1
351 Views
Last Modified: 2013-11-26
Instead of using the error message "Error" I would like to change the textbox txtzipcode  background red using CSS can this be done.

<form id="form1" runat="server">
    <div>
   
        <asp:TextBox ID="txtzipcode" runat="server"></asp:TextBox>
        <asp:RegularExpressionValidator ID="ValZipcode" runat="server"
            BorderStyle="None" ControlToValidate="txtzipcode"
            ErrorMessage="Error"
           
           
            ValidationExpression="^((\d{5}-\d{4})|(\d{5})|([AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[A-Za-z]\s?\d[A-Za-z]\d))$"
            ForeColor="#FF3300"></asp:RegularExpressionValidator>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
   
    </div>
    </form>
0
Comment
Question by:WDJ98338
1 Comment
 
LVL 13

Accepted Solution

by:
numberkruncher earned 500 total points
ID: 24127337
I don't think that you can do that with the regular expression validator control; however it is very easy to add this functionality to your button click handler.
protected void Button1_Click(object sender, EventArgs e)

{

    string pattern = @"^((\d{5}-\d{4})|(\d{5})|([AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[A-Za-z]\s?\d[A-Za-z]\d))$";
 

    if (!Regex.IsMatch(txtzipcode.Text, pattern, RegexOptions.Compiled))

    {

        txtzipcode.BackColor = Color.Red;

    }

}

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now