Solved

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

Posted on 2009-04-12
1
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
This video teaches users how to migrate an existing Wordpress website to a new domain.

695 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