user3340627
user3340627

Reputation: 3143

Set Textbox border color to red if Validation fails (Extended control)

I've extended my textbox control to add a regular expression validator for the date format.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Myapplication.App_Code
{
    public class DateTextBox  : TextBox
    {
        private RegularExpressionValidator regexval;
        public string InvalidDate="Incorrect date format, must be dd/mm/yyyy";
        public string ClientScript = "true";

        protected override void OnInit(EventArgs e)
        {
            regexval = new RegularExpressionValidator();
            regexval.ControlToValidate = this.ID;
            regexval.ErrorMessage = this.InvalidDate;
            regexval.EnableClientScript = (this.ClientScript.ToLower() != "false");
            regexval.ValidationExpression = "^(0[1-9]|[12][0-9]|3[01])[-/.](0[1-9]|1[012])[-/.](19|20)\\d\\d$";
            //regexval.Text = "*Incorrect date format";
            regexval.ForeColor = System.Drawing.Color.Red;
            regexval.ValidationGroup = "Tab1";
            Controls.Add(regexval);

            base.OnInit(e);
        }
        protected override void OnPreRender(EventArgs e)
        {
            Attributes.Add("placeholder", "dd/mm/yyyy");

            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            regexval.RenderControl(writer);
        }
    }
}

If the dynamically added Regular expression validation fails, I need to fire some event in which i would change the textbox color. So far I can only display "Text" property or "Error Messsage" when not valid using the out-of-the-box properties.

Upvotes: 0

Views: 826

Answers (1)

Antonne Tulloch
Antonne Tulloch

Reputation: 33

I'm not sure if this is what you're looking for but there are javascript libraries that will do the same thing for you. You could try this one http://semantic-ui.com/behaviors/form.html

That's if javascript will work for you.

Upvotes: 1

Related Questions