zoukankan      html  css  js  c++  java
  • PhoneValidator.cs

    // Title: Building ASP.NET Server Controls
    //
    // Chapter: 9 - Integrating Client-Side Script
    // File: PhoneValidator.cs
    // Written by: Dale Michalk and Rob Cameron
    //
    // Copyright ?2003, Apress L.P.
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text.RegularExpressions;

    namespace ControlsBookLib.Ch09
    {
        public enum PhoneNumberType
        {
            US = 0,
            Intl = 1
        }

        [ ToolboxData("<{0}:PhoneValidator NumberType=US runat=server></{0}:PhoneValidator>") ]
        public class PhoneValidator : BaseValidator
        {        
            public virtual PhoneNumberType NumberType
            {
                get
                {
                    object numType = ViewState["NumberType"];
                    if (numType == null)
                        return PhoneNumberType.US;
                    else
                        return (PhoneNumberType) numType;
                }
                set
                {
                    ViewState["NumberType"] = value;
                }       
            }

            const string US_PHONE_REGEX = @"^\(?\d{3}\)?(\s|-)\d{3}\-\d{4}$";
            const string INTL_PHONE_REGEX =@"^\d(\d|-){7,20}$";

            private string PhoneRegex
            {
                get
                {
                    // read only property selects the appropriate
                    // regular expression to use for US or
                    // international phone based on the NumberType
                    // property setting
                    if (this.NumberType == PhoneNumberType.Intl)
                          return INTL_PHONE_REGEX;
                      else
                          return US_PHONE_REGEX;
                }
            }
            
            protected override void AddAttributesToRender(HtmlTextWriter writer)
            {
                base.AddAttributesToRender (writer);

                // BaseValidator has logic to determine uplevel browser
                // client capability
                if (base.RenderUplevel)
                {
                    // link into the ClientValidation API of ASP.NET and
                    // reuse its client-side Regex evaluation code
                    writer.AddAttribute("evaluationfunction","RegularExpressionValidatorEvaluateIsValid");
                    writer.AddAttribute("validationexpression",PhoneRegex);
                }
            }

            protected override bool EvaluateIsValid()
            {
                // BaseValidator provides GetControlValidationValue
                // to make it easy to grab the controls value for you
                string number = base.GetControlValidationValue(
                    base.ControlToValidate);
                
                // enforce the rule that we don't evaluate null
                // or empty values in non-RequiredFieldValidator
                // controls
                if (number.Trim() != "")
                {    
                    // use the regular expression engine of ASP.NET
                    // to evaluate the phone number according to the
                    // selected Phone type regular expression
                    Regex expr = new Regex(PhoneRegex);
                    MatchCollection matches = expr.Matches(number, 0);
                    return (matches != null && matches[0].Value == number);
                }
                else
                    return true;
            }
        }
    }
  • 相关阅读:
    PHP+MySQL
    Appstore排名前十的程序员应用软件
    架构师的平凡之路
    程序员,如何三十而立?
    不懂技术也可以轻松开发一款APP
    php语法学习:轻松看懂PHP语言
    你真的了解软件测试行业吗?
    十个程序员必备的网站推荐
    从更高点看软件开发的侧重点
    php如何实现文件下载
  • 原文地址:https://www.cnblogs.com/shihao/p/2498668.html
Copyright © 2011-2022 走看看