javascript教程 AJAX常用技术 AJAX框架技术
返回首页
当前位置: 首页 > AJAX教程 > javascript教程 >

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

时间:2009-09-26 09:13来源:未知 作者:.Net 泥水佬 点击:我要投稿  高质量的ASP.NET空间,完美支持1.0/2.0/3.5/4.0/MVC等

做WEB应用在HTML里写JavaScript(JS)验证数据是正常的事情,但VS.NET对JS的智能感知支持远没有C#这么强大,因此在写JS写多了也是麻烦的事情。为了方便所以写了一个Helper方便生成js验证代码。

先看下在应用中的代码:

 

 <form action="Register.aspx" method="post" onsubmit="return VRegister()">
    <p>用户名:<%=HtmlHelper.Input(InputType.text, "UserName").Value(view.User.UserName).Id("user")%><label id="usertip" /></p>
    <p>密码:<%=HtmlHelper.Input(InputType.password,"UserPWD").Id("pwd") %><label id="pwdtip" /></p>
    <p>确认密码:<%=HtmlHelper.Input(InputType.password,"RPWD").Id("rpwd") %><label id="rpwdtip" /></p>
    <p>邮件地址:<%=HtmlHelper.Input(InputType.text, "EMail").Value(view.User.EMail).Id("email")%><label id="emailtip" /></p>
    <p><input type="submit" value="注册" /></p>
    <p></p>
    </form>
       
        <%JSValidator jsv = new JSValidator("VRegister");
          jsv.Add(jsv.Create("user", "usertip").NotNull("请输入用户名!", null),
              jsv.Create("pwd","pwdtip").NotNull("请输入密码!",null),
              jsv.Create("rpwd","rpwdtip").NotNull("请输入确认密码!",null).StringCompare("pwd", CompareType.eq,"密码不一致!",null),
              jsv.Create("email","emailtip").NotNull("请输入邮件地址!",null).EMail("非法邮件地址!",null));
        %>
        <%=jsv %>

 

 

灰色部分代码就是验证Helper的代码,通过C#代码能够很快的完成输写。
实际的应用效果:

 

JS功能代码
 

 
function Validator(){}
Validator.prototype.Success = true;
Validator.prototype.Execute = null;
Validator.prototype.Message = null;
//值不为空
Validator.prototype.NotNull=function(value) {
    this.Success= value != null && value != '' && value != undefined;
}
//正则匹配
Validator.prototype.MatchRegex= function(value, parent) {
    var reg =  new RegExp(parent);
    reg.ignoreCase = true;
    if (value.match(reg) == null) {
        this.Success= false;
    }
    else{
        this.Success= true;
    }
}
//执行验证
Validator.prototype.Do=function(call)
{
    this.Success = true;
    if(this.Execute !=null)
      this.Execute();
}
//字符长度区间
Validator.prototype.LengthRegion= function(value,min,max)
{
    if(!value)
    {
         this.Success= true;
    }
    else
    {
         var lvalue = value.toString().length;
         var min = __ValidatorConvert(min,"number");
         var max = __ValidatorConvert(max,"number");
         if(min !=null && max !=null)
             this.Success= __CompareValue(lvalue,min,"req")&& __CompareValue(lvalue,max,"leq");
          else
             this.Success= __CompareValue(lvalue,min,"req")|| __CompareValue(lvalue,max,"leq");
    }
}
//数字大小区间
Validator.prototype.NumberRegion =function(value,min,max) {
    if(!value)
    {
        this.Success= true;
    }
    else
    {
        var lvalue = __ValidatorConvert(value,"number");
        if(!lvalue)
        {
           this.Success= false;
        }
        else
        {
           
            var min = __ValidatorConvert(min,"number");
            var max = __ValidatorConvert(max,"number");
           
            if(min !=null && max !=null)
                this.Success= __CompareValue(lvalue,min,"req")&& __CompareValue(lvalue,max,"leq");
            else
                this.Success= __CompareValue(lvalue,min,"req")|| __CompareValue(lvalue,max,"leq");
        }
    }
}
//比较数字
Validator.prototype.NumberCompare = function(leftvalue,rightvalue,type){
    var lvalue = __ValidatorConvert(leftvalue,"number");
    var rvalue = __ValidatorConvert(rightvalue,"number");
    this.Success= __CompareValue(lvalue,rvalue,type);
}

//日期大小区间
Validator.prototype.DataRegion = function(value,min,max)
{
    if(!value)
    {
        this.Success= true;
    }
    else
    {
        var lvalue = __ValidatorConvert(value,"date");
        if(!lvalue)
        {
            this.Success = false;
        }
        else
        {
            var min = __ValidatorConvert(min,"date");
            var max = __ValidatorConvert(max,"date");
            if(min !=null && max !=null)
                this.Success= __CompareValue(lvalue,min,"req")&& __CompareValue(lvalue,max,"leq");
            else
                this.Success= __CompareValue(lvalue,min,"req")|| __CompareValue(lvalue,max,"leq");
        }
    }
}
//比较日期
Validator.prototype.DateCompare = function(leftvalue,rightvalue,type){
        var lvalue = __ValidatorConvert(leftvalue,"Date");
        var rvalue = __ValidatorConvert(rightvalue,"Date");
        this.Success= __CompareValue(lvalue,rvalue,type);
}
//匹配邮件
Validator.prototype.EMail = function(value){
    this.MatchRegex(value,'^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$');
}
//字符比较
Validator.prototype.StringCompare = function(leftvalue,rightvalue,type){
    this.Success= __CompareValue(leftvalue,rightvalue,type);
}

//选择项
Validator.prototype.SelectCheckbox= function(controls,selectcount)
{
    var selects =0;
    for(var i =0;i<controls.length;i++)
    {
       if($('#'+controls[i]).attr('checked'))
       {
            selects++;
       }
    }
    this.Success= selects>= selectcount;
}
//选择项
Validator.prototype.SelectCheckboxFromTo=function(control,from,to,selectcount)
{
    var selects =0;
    for(var i =from;i<to;i++)
    {
       if($('#'+controls[i]).attr('checked'))
       {
            selects++;
       }
    }
    this.Success= selects>= selectcount;
}
//值是否为null
function __IsNull(value)
{
 
    if(!value)
        return true;
    return false;
}
//比较值函数
function __CompareValue(leftvalue,rightvalue,compareType)
{
    if(__IsNull(leftvalue)  || __IsNull(rightvalue))
        return false;
    if(compareType=="eq")
    {
        return leftvalue == rightvalue;
    }
    else if(compareType =="neq")
    {
        return leftvalue != rightvalue;
    }
    else if(compareType =="le")
    {
        return leftvalue < rightvalue;
    }
    else if(compareType =="leq")
    {
        return leftvalue <= rightvalue;
    }
    else if(compareType =="ri")
    {
        return leftvalue > rightvalue;
    }
    else if(compareType =="req")
    {
        return leftvalue >= rightvalue;
    }
    else
    {
        return false;
    }
   
}
//转换值函数
function __ValidatorConvert(value, dataType) {
    var num,exp,m;
    var year,month,day
    if(value == null || value =="")
        return null;
    if(dataType=="int")
    {
        exp=/^[-\+]?\d+$/;
        if (value.match(exp) == null)
            return null;
        num = parseInt(value, 10);
        return (isNaN(num) ? null : num);
    }
    else if(dataType =="number")
    {
        exp=/^[-\+]?((\d+)|(\d+\.\d+))$/;
        if (value.match(exp) == null)
            return null;
        num = parseFloat(value);
        return (isNaN(num) ? null : num);
    }
    else if(dataType =="date")
    {
        exp=/^(\d{4})([-/]?)(\d{1,2})([-/]?)(\d{1,2})$/
        m = value.match(exp);
        if (m == null)
        {
            exp=/^(\d{1,2})([-/]?)(\d{1,2})([-/]?)(\d{4})$/
            m = value.match(exp);
            if(m== null)
                return null;
            year = m[5];
            month = m[1];
            day =m[3];
           
        }
        else
        {
            year = m[1];
            month =m[3];
            day = m[5];
        }
        try
        {
            num = new Date(year,month,day);
        }
        catch(e)
        {
            return null;
        }
        return num;
    }
    else
    {
        return value.toString();
    }
}

c# helper代码

 public class JSValidator
    {
        public JSValidator(string funname)
        {
            mFunctionName = funname;

        }
        private string mFunctionName;
        private StringBuilder mScript = new StringBuilder();
      
        public Validator Create(string control, string tipcontrol)
        {
            Validator item = new Validator(control, tipcontrol);
            item.ShowErrorTip = ShowErrorTip;
          
            return item;
        }
        public Validator Create(string control)
        {
            return Create(control, null);
        }
        public JSValidator Add(params Validator[] items)
        {
            Items.AddRange(items);
            return this;
        }
        public bool ShowErrorTip
        {
            get;
            set;
        }
        private List<Validator> mItems = new List<Validator>();
        public List<Validator> Items
        {
            get
            {
                return mItems;
            }
        }
        private void WriteLine(string value)
        {
            mScript.AppendLine(value);
        }
        private void WriteLine(string format, params object[] values)
        {
            mScript.AppendLine(string.Format(format, values));
        }
        public override string ToString()
        {
            mScript = new StringBuilder();
            WriteLine("<script>");
            foreach(Validator item in Items)
            {
                mScript.AppendLine(item.ToString());
               
            }
            WriteLine("function {0}()",mFunctionName);
            WriteLine("{");
            WriteLine("\tvar _msg = '';");
            WriteLine("\tvar _success = true;");
            foreach (Validator item in Items)
            {
                WriteLine("\t_{0}.Do();", item.GetHashCode());

            }
            foreach(Validator item in Items)
            {
                WriteLine("\t _success = _success && _{0}.Success;",item.GetHashCode());
                WriteLine("\tif(_{0}.Message !=null)",item.GetHashCode());
                WriteLine("\t\t_msg+=_{0}.Message;",item.GetHashCode());
            }
            WriteLine("\tif(!_success&&_msg!=''){alert(_msg);}");
            WriteLine("\treturn _success");
            WriteLine("}");
            WriteLine("$(document).ready(function(){");
            foreach (Validator item in Items)
            {
                WriteLine("$('#" + item.Control + "').change(function(){_" + item.GetHashCode() + ".Do();});\r\n");

            }
            WriteLine("});");
            WriteLine("</script>");
         
            return mScript.ToString();
        }
    }
    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("\t\t\t$('#{0}').attr('title','{1}');", mControl, msg);
            if (string.IsNullOrEmpty(mTipControl))
            {
                WriteLine("\t\t\t this.Message='{0}\\r\\n';", msg);
            }
            else
            {
                if(ShowErrorTip)
                    WriteLine("\t\t\t$('#{0}').text('{1}');", mTipControl, msg);
                WriteLine("\t\t\t$('#{0}').removeClass('SuccessTip');", mTipControl);
                WriteLine("\t\t\t$('#{0}').addClass('ErrorTip');", mTipControl);
            }
          
           
        }
        private void SetSuccessMessage(string msg)
        {
            WriteLine("\t\t\t$('#{0}').attr('title','{1}');", mControl, "");
            if (!string.IsNullOrEmpty(mTipControl))
            {
                WriteLine("\t\t\t$('#{0}').text('{1}');", mTipControl,"");
                if (ShowErrorTip)
                    WriteLine("\t\t\t$('#{0}').text('{1}');", mTipControl, msg);
                WriteLine("\t\t\t$('#{0}').removeClass('ErrorTip');", mTipControl);
                WriteLine("\t\t\t$('#{0}').addClass('SuccessTip');", mTipControl);
            }
          
        }
        public Validator NotNull(string errmsg,string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.NotNull($('#{1}').val());", GetHashCode(), mControl);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator DateRegion(string max, string min, string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.DataRegion($('#{1}').val(),'{2}','{3}');", GetHashCode(), mControl,min,max);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator NumberRegion(string max, string min, string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.NumberRegion($('#{1}').val(),'{2}','{3}');", GetHashCode(), mControl, min, max);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator LengthRegion(string max, string min, string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.LengthRegion($('#{1}').val(),'{2}','{3}');", GetHashCode(), mControl, min, max);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator EMail(string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.EMail($('#{1}').val());", GetHashCode(), mControl);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator Regex(string regex, string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.MatchRegex($('#{1}').val(),'{2}');", GetHashCode(), mControl, regex);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator SelectCheckbox(int selectcount, string errmsg, string successmsg, params string[] controls)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.SelectCheckbox($('#{1}').val(),'{2}');", GetHashCode(), mControl, selectcount);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator StringCompare(string rc, CompareType type, string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.StringCompare($('#{1}').val(),$('#{2}').val(),'{3}');", GetHashCode(), Control,rc, type.ToString());
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator DateCompare(string rc, CompareType type, string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.DateCompare($('#{1}').val(),$('#{2}').val(),'{3}');", GetHashCode(), Control, rc, type.ToString());
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator NumberCompare(string rc, CompareType type, string errmsg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.NumberCompare($('#{1}').val(),$('#{2}').val(),'{3}');", GetHashCode(), Control, rc, type.ToString());
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }
        public Validator SelectCheckboxFromTo(string control, int from, int to, int selectcount, string msg, string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            WriteLine("\t\t_{0}.SelectCheckboxFromTo('{1}','{2}','{3}','{4}');", GetHashCode(), mControl, from,to,selectcount);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(msg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetErrorMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
            return this;
        }

        public override string ToString()
        {
            return mScript.ToString()+"}\r\n";
        }
    }

    public enum CompareType
    {
       
        eq,
        neq,
        le,
        leq,
        ri,
        req
    }

 

重构后的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("\t\t\t$('#{0}').attr('title','{1}');", mControl, msg);
            if (string.IsNullOrEmpty(mTipControl))
            {
                WriteLine("\t\t\t this.Message='{0}\\r\\n';", msg);
            }
            else
            {
                if(ShowErrorTip)
                    WriteLine("\t\t\t$('#{0}').text('{1}');", mTipControl, msg);
                WriteLine("\t\t\t$('#{0}').removeClass('SuccessTip');", mTipControl);
                WriteLine("\t\t\t$('#{0}').addClass('ErrorTip');", mTipControl);
            }
          
           
        }
        private void SetSuccessMessage(string msg)
        {
            WriteLine("\t\t\t$('#{0}').attr('title','{1}');", mControl, "");
            if (!string.IsNullOrEmpty(mTipControl))
            {
                WriteLine("\t\t\t$('#{0}').text('{1}');", mTipControl,"");
                if (ShowErrorTip)
                    WriteLine("\t\t\t$('#{0}').text('{1}');", mTipControl, msg);
                WriteLine("\t\t\t$('#{0}').removeClass('ErrorTip');", mTipControl);
                WriteLine("\t\t\t$('#{0}').addClass('SuccessTip');", mTipControl);
            }
          
        }
        protected void OutScript(Action<Validator> callfun,string errmsg,string successmsg)
        {
            WriteLine("\tif(_{0}.Success)", GetHashCode());
            WriteLine("\t{");
            callfun(this);
            WriteLine("\t\tif(!_{0}.Success)", GetHashCode());
            WriteLine("\t\t{");
            SetErrorMessage(errmsg);
            WriteLine("\t\t}");
            WriteLine("\t\telse{");
            SetSuccessMessage(successmsg);
            WriteLine("\t\t}");
            WriteLine("\t}");
        }
        public Validator NotNull(string errmsg,string successmsg)
        {

            OutScript(o => { WriteLine("\t\t_{0}.NotNull($('#{1}').val());", GetHashCode(), mControl); },errmsg,successmsg);
            return this;
        }
        public Validator DateRegion(string max, string min, string errmsg, string successmsg)
        {

            OutScript(o => { WriteLine("\t\t_{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("\t\t_{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("\t\t_{0}.LengthRegion($('#{1}').val(),'{2}','{3}');", GetHashCode(), mControl, min, max); }, errmsg, successmsg);
            return this;
        }
        public Validator EMail(string errmsg, string successmsg)
        {

            OutScript(o => { WriteLine("\t\t_{0}.EMail($('#{1}').val());", GetHashCode(), mControl); }, errmsg, successmsg);
            return this;
        }
        public Validator Regex(string regex, string errmsg, string successmsg)
        {

            OutScript(o => { WriteLine("\t\t_{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("\t\t_{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("\t\t_{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("\t\t_{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("\t\t_{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("\t\t_{0}.SelectCheckboxFromTo('{1}','{2}','{3}','{4}');", GetHashCode(), mControl, from, to, selectcount); }, errmsg, successmsg);
            return this;
        }

        public override string ToString()
        {
            return mScript.ToString()+"}\r\n";
        }
    }

 

扩展Ajax功能

 Validator.prototype.Ajax = function(url,params)
{
     var result = $.ajax({
         url: url,
         async: false,
         data:__CreatePostData(params)
     }).responseText;
     if($(result).find('Exception').text())
        this.Success = false;
     else
        this.Success = true;
}
function __CreatePostData(expression)
{

        var param = new Object();
        if(expression != null && expression !='')
        {
                var properties = expression.split('&');
                var namevalue,execute;
                for(i =0;i<properties.length;i++)
                {
                    namevalue = properties[i].split('=');
                    if(namevalue.length ==2)
                    {
                        if(namevalue[1].indexOf('#') >=0)
                        {
                            execute ='param.' + namevalue[0]+'=$(\"'+ namevalue[1]+'\").val()';
                        }
                        else
                        {
                            execute ='param.' + namevalue[0]+'=\"'+ namevalue[1]+'\"';
                        }
                        eval(execute);
                    }
                }
        }
        param.PostTime = new Date().toString();
        return param;
}

 

   public Validator Ajax(string url, string data, string errmsg, string successmsg)
        {
            OutputScript(o => { WriteLine("\t\t_{0}.Ajax('{1}','{2}');", GetHashCode(), url,data); }, errmsg, successmsg);
            return this;
        }

 
本站推荐文章:
本站热点文章:
顶一下
(5)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色 情、暴力、反动的言论。
评价:
表情:
用户名:密码: 验证码:点击我更换图片