如果验证失败,则将文本框边框颜色设置为红色(扩展控制)

时间:2015-01-04 15:43:33

标签: c# asp.net

我已扩展文本框控件,为日期格式添加正则表达式验证程序。

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);
        }
    }
}

如果动态添加的正则表达式验证失败,我需要触发一些我将更改文本框颜色的事件。到目前为止,我只能显示" Text"财产或"错误消息"使用开箱即用的属性时无效。

1 个答案:

答案 0 :(得分:1)

我不确定这是否是您正在寻找的,但有些javascript库会为您做同样的事情。你可以尝试这个http://semantic-ui.com/behaviors/form.html

如果javascript适合你的话。