InsensitiveRegularExpressionValidator

    /// <summary>
    /// Simple Regular Expression validator with IgnoreCase=true for client and server-side evaluation
    /// </summary>
    /// <remarks>
    /// FINALLY, the Longhorn documentation that keeps coming up in search results is *useful*.
    /// Looks like MS is going to improve their documentation standard enormously for VS.NET2005
    /// <para>Validator Control Samples
    /// http://longhorn.msdn.microsoft.com/lhsdk/ndp/cpconvalidatorcontrolsamples.aspx </para>
    /// <para>Regular Expression Validator Control Sample
    /// http://longhorn.msdn.microsoft.com/lhsdk/ndp/cpconregularexpressionvalidatorcontrolsample.aspx </para>
    /// <para>
    /// Client-Side Functionality in a Server Control
    /// http://longhorn.msdn.microsoft.com/lhsdk/ndp/cpconclient-sidefunctionalityinservercontrol.aspx </para>
    /// </remarks>
    public class InsensitiveRegularExpressionValidator : System.Web.UI.WebControls.BaseValidator {
        /// <summary>String to validate using <see cref="ValidationExpression"/>.</summary>
        private string _evalString;
        /// <summary>Regular Expression to use for processing</summary>
        private string _regexString;
        /// <summary>Key used when adding script to the <see cref="Page"/> to ensure it's not duplicated</summary>
        private const string _validatorIncludeScriptKey = "InsensitiveRegularExpressionScript";
        /// <summary>Client-side regular expression processed based on .NET Framework function in
        /// C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\WebUIValidation.js </summary>
        /// <remarks>Information on case-insensitivity in Jscript Regular Expressions
        /// http://www.webreference.com/js/column5/methods.html </remarks>
        private string _includeScript = @"
<script language=""javascript"">
<!--
// from v1.1.4322/WebUIValidation.js - uses ValidatorGetValue() and ValidatorTrim()
// so we'll have a problem if the aspnet_client jscript isn't loaded...
function InsensitiveRegularExpressionValidatorEvaluateIsValid (val){
    var value = ValidatorGetValue(val.controltovalidate);
    if (ValidatorTrim(value).length == 0)
        return true;        
    var rx = new RegExp(val.validationexpression, 'i');        /* Case insensitive on the Client-side */
    var matches = rx.exec(value);
    return (matches != null && value == matches[0]);
}
// -->
</script>
";
        /// <summary>Validation expression, typically set in ASPX declaration</summary>
        public string ValidationExpression {
            get{return _regexString;}
            set{_regexString = value;}
        }
        
        /// <summary>Constructor - used to force disable client-side script during testing</summary>
        public InsensitiveRegularExpressionValidator() {
            //base.EnableClientScript = false;
        }

        /// <summary>Check if the ControlToValidate property is set to a valid control</summary>
        /// <returns>true if the ControlToValidate is set to a TextBox or HtmlInputControl subclass</returns>
        protected override bool ControlPropertiesValid() {
            Control ctrl = FindControl(ControlToValidate);
            if ( null != ctrl ) {
                if (ctrl is System.Web.UI.WebControls.TextBox) {
                    _evalString = ((System.Web.UI.WebControls.TextBox) ctrl).Text;
                    return ( null != _evalString );
                } else if (ctrl is System.Web.UI.HtmlControls.HtmlInputControl) {
                    _evalString = ((System.Web.UI.HtmlControls.HtmlInputControl) ctrl).Value;
                    return ( null != _evalString );
                }
                return false;
            } else
                return false;
        }

        /// <summary>Check if the ControlToValidate property is set to a valid control</summary>
        /// <returns>true if the ControlToValidate is set to a TextBox or HtmlInputControl subclass</returns>
        protected override bool EvaluateIsValid() {
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(ValidationExpression
                        , System.Text.RegularExpressions.RegexOptions.IgnoreCase);        /* Case insensitive on the Server-side */
            return regex.IsMatch(_evalString) ;
        }

        /// <summary>Register this control for inclusion in the page Validation process</summary>
        protected override void RegisterValidatorDeclaration() {
            string element = "document.getElementById(\"" + ClientID + "\")";
            Page.RegisterArrayDeclaration("Page_Validators", element);
        }

        /// <summary>Add the actual script function to the <see cref="Page"/>, once only.
        /// The control knows the name of the function *inside* this script using the 'validationexpression'
        /// attribute set in the <see cref="AddAttributesToRender"/> method</summary>
        protected new void RegisterValidatorCommonScript () {
            if (!Page.IsClientScriptBlockRegistered(_validatorIncludeScriptKey)) {
                Page.RegisterClientScriptBlock(_validatorIncludeScriptKey, _includeScript);
            }

        }
        /// <summary>Ensure the <see cref="Page"/> contains the script required for the control to work</summary>
        protected override void OnPreRender(EventArgs e) {
            this.RegisterValidatorCommonScript();
            base.OnPreRender (e);
        }

        /// <summary>Ensure the Framework Validation code knows what Javascript function to call
        /// by setting the 'validationexpression' attribute for this control. This means
        /// we MUST have added the actual Javascript code to the page in <see cref="RegisterValidatorCommonScript"/></summary>
        protected override void AddAttributesToRender(HtmlTextWriter writer) {
            base.AddAttributesToRender(writer);
            if (RenderUplevel) {
                writer.AddAttribute("evaluationfunction", "InsensitiveRegularExpressionValidatorEvaluateIsValid");
                if (ValidationExpression.Length > 0) {
                    writer.AddAttribute("validationexpression", ValidationExpression);
                }
            }
        }
    } // InsensitiveRegularExpressionValidator