WEB开发网
开发学院网页设计JavaScript 实现一个JavaScript验证的Asp.net Helper 阅读

实现一个JavaScript验证的Asp.net Helper

 2009-09-29 00:00:00 来源:WEB开发网   
核心提示: 重构后的Validator代码publicclassValidator{publicValidator(stringcontrol,stringtipcontrol){mControl=control;mTipControl=tipcontrol;WriteLine("var_{0}=

重构后的Validator代码

 public class Validator
    {
        public Validator(string control,string tipcontrol)
        {
            mControl = control;
            mTipControl = tipcontrol;
            WriteLine("var _{0}= new Validator();", GetHashCode());
            WriteLine("_{0}.Execute=function()",GetHashCode());
            WriteLine("{");
        }
        private string mControl;
        private string mTipControl;
        public string Control
        {
            get
            {
                return mControl;
            }
        }
        public bool ShowErrorTip
        {
            get;
            set;
        }
        private StringBuilder mScript = new StringBuilder();
        protected void WriteLine(string value)
        {
            mScript.AppendLine(value);
        }
        protected void WriteLine(string format, params object[] values)
        {
            mScript.AppendLine(string.Format(format, values));
        }
        private void SetErrorMessage(string msg)
        {
            //removeClass,addClass
            WriteLine("ttt$('#{0}').attr('title','{1}');", mControl, msg);
            if (string.IsNullOrEmpty(mTipControl))
            {
                WriteLine("ttt this.Message='{0}\r\n';", msg);
            }
            else
            {
                if(ShowErrorTip)
                    WriteLine("ttt$('#{0}').text('{1}');", mTipControl, msg);
                WriteLine("ttt$('#{0}').removeClass('SuccessTip');", mTipControl);
                WriteLine("ttt$('#{0}').addClass('ErrorTip');", mTipControl);
            }
           
            
        }
        private void SetSuccessMessage(string msg)
        {
            WriteLine("ttt$('#{0}').attr('title','{1}');", mControl, "");
            if (!string.IsNullOrEmpty(mTipControl))
            {
                WriteLine("ttt$('#{0}').text('{1}');", mTipControl,"");
                if (ShowErrorTip)
                    WriteLine("ttt$('#{0}').text('{1}');", mTipControl, msg);
                WriteLine("ttt$('#{0}').removeClass('ErrorTip');", mTipControl);
                WriteLine("ttt$('#{0}').addClass('SuccessTip');", mTipControl);
            }
           
        }
        protected void OutScript(Action<Validator> callfun,string errmsg,string successmsg)
        {
            WriteLine("tif(_{0}.Success)", GetHashCode());
            WriteLine("t{");
            callfun(this);
            WriteLine("ttif(!_{0}.Success)", GetHashCode());
            WriteLine("tt{");
            SetErrorMessage(errmsg);
            WriteLine("tt}");
            WriteLine("ttelse{");
            SetSuccessMessage(successmsg);
            WriteLine("tt}");
            WriteLine("t}");
        }
        public Validator NotNull(string errmsg,string successmsg)
        {
            OutScript(o => { WriteLine("tt_{0}.NotNull($('#{1}').val());", GetHashCode(), mControl); },errmsg,successmsg);
            return this;
        }
        public Validator DateRegion(string max, string min, string errmsg, string successmsg)
        {
            OutScript(o => { WriteLine("tt_{0}.DataRegion($('#{1}').val(),'{2}','{3}');", GetHashCode(), mControl, min, max); }, errmsg, successmsg);
            return this;
        }
        public Validator NumberRegion(string max, string min, string errmsg, string successmsg)
        {
            OutScript(o => { WriteLine("tt_{0}.NumberRegion($('#{1}').val(),'{2}','{3}');", GetHashCode(), mControl, min, max); }, errmsg, successmsg);
            return this;
        }
        public Validator LengthRegion(string max, string min, string errmsg, string successmsg)
        {
            OutScript(o => { WriteLine("tt_{0}.LengthRegion($('#{1}').val(),'{2}','{3}');", GetHashCode(), mControl, min, max); }, errmsg, successmsg);
            return this;
        }
        public Validator EMail(string errmsg, string successmsg)
        {
            OutScript(o => { WriteLine("tt_{0}.EMail($('#{1}').val());", GetHashCode(), mControl); }, errmsg, successmsg);
            return this;
        }
        public Validator Regex(string regex, string errmsg, string successmsg)
        {
            OutScript(o => { WriteLine("tt_{0}.MatchRegex($('#{1}').val(),'{2}');", GetHashCode(), mControl, regex); }, errmsg, successmsg);
            return this;
        }
        public Validator SelectCheckbox(int selectcount, string errmsg, string successmsg, params string[] controls)
        {
            OutScript(o => { WriteLine("tt_{0}.SelectCheckbox($('#{1}').val(),'{2}');", GetHashCode(), mControl, selectcount); }, errmsg, successmsg);
            return this;
        }
        public Validator StringCompare(string rc, CompareType type, string errmsg, string successmsg)
        {
            OutScript(o => { WriteLine("tt_{0}.StringCompare($('#{1}').val(),$('#{2}').val(),'{3}');", GetHashCode(), Control, rc, type.ToString()); }, errmsg, successmsg);
            return this;
        }
        public Validator DateCompare(string rc, CompareType type, string errmsg, string successmsg)
        {
 
            OutScript(o => { WriteLine("tt_{0}.DateCompare($('#{1}').val(),$('#{2}').val(),'{3}');", GetHashCode(), Control, rc, type.ToString()); }, errmsg, successmsg);
            return this;
        }
        public Validator NumberCompare(string rc, CompareType type, string errmsg, string successmsg)
        {
 
            OutScript(o => { WriteLine("tt_{0}.NumberCompare($('#{1}').val(),$('#{2}').val(),'{3}');", GetHashCode(), Control, rc, type.ToString()); }, errmsg, successmsg);
            return this;
        }
        public Validator SelectCheckboxFromTo(string control, int from, int to, int selectcount, string errmsg, string successmsg)
        {
            OutScript(o => { WriteLine("tt_{0}.SelectCheckboxFromTo('{1}','{2}','{3}','{4}');", GetHashCode(), mControl, from, to, selectcount); }, errmsg, successmsg);
            return this;
        }
        public override string ToString()
        {
            return mScript.ToString()+"}rn";
        }
    }

上一页  1 2 3 4 5  下一页

Tags:实现 一个 JavaScript

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接