当前位置:w88优德官网网文章中心网站设计网页设计 → 实现一个JavaScript验证的Asp.net Helper

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

减小字体 增大字体 作者:admin  来源:www.hack50.com  发布时间:2012-4-14 8:37:44

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

  先看下在应用中的代码:

    <form action="Register.aspx" method="post" >
    <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#代码能够很快的完成输写。

  实际的应用效果:

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

  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("tt_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();});rn");
            }
            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("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);
            }
           
        }
        public Validator NotNull(string errmsg,string successmsg)
        {
            WriteLine("tif(_{0}.Success)", GetHashCode());
            WriteLine("t{");
            WriteLine("tt_{0}.NotNull($('#{1}').val());", GetHashCode(), mControl);
            WriteLine("ttif(!_{0}.Success)", GetHashCode());
            WriteLine("tt{");
            SetErrorMessage(errmsg);
            WriteLine("tt}");
            WriteLine("ttelse{");
            SetSuccessMessage(successmsg);
            WriteLine("tt}");
            WriteLine("t}");
            return this;
        }
        public Validator DateRegion(string max, string min, string errmsg, string successmsg)
        {
            WriteLine("tif(_{0}.Success)", GetHashCode());
            WriteLine("t{");
            WriteLine("tt_{0}.DataRegion($('#{1}').val(),'{2}','{3}');", GetHashCode(), mControl,min,max);
            WriteLine("ttif(!_{0}.Success)", GetHashCode());
            WriteLine("tt{");
            SetErrorMessage(errmsg);
            WriteLine("tt}");
            WriteLine("ttelse{");
            SetSuccessMessage(successmsg);
            WriteLine("tt}");
            WriteLine("t}");
            return this;
        }
        public Validator NumberRegion(string max, string min, string errmsg, string successmsg)
        {
            WriteLine("tif(_{0}.Success)", GetHashCode());
            WriteLine("t{");
            WriteLine("tt_{0}.NumberRegion($('#{1}').val(),'{2}','{3}');", GetHashCode(), mControl, min, max);
            WriteLine("ttif(!_{0}.Success)", GetHashCode());
            WriteLine("tt{");
            SetErrorMessage(errmsg);
            WriteLine("tt}");
            WriteLine("ttelse{");
            SetSuccessMessage(successmsg);
            WriteLine("tt}");
            WriteLine("t}");
            return this;
        }
        public Validator LengthRegion(string max, string min, string errmsg, string successmsg)
        {
            WriteLine("tif(_{0}.Success)", GetHashCode());
            WriteLine("t{");
            WriteLine("tt_{0}.LengthRegion($('#{1}').val(),'{2}','{3}');", GetHashCode(), mControl, min, max);
            WriteLine("ttif(!_{0}.Success)", GetHashCode());
            WriteLine("tt{");
            SetErrorMessage(errmsg);
            WriteLine("tt}");
            WriteLine("ttelse{");
            SetSuccessMessage(successmsg);
            WriteLine("tt}");
            WriteLine("t}");
            return this;
        }
        public Validator EMail(string errmsg, string successmsg)
        {
            WriteLine("tif(_{0}.Success)", GetHashCode());
            WriteLine("t{");
            WriteLine("tt_{0}.EMail($('#{1}').val());", GetHashCode(), mControl);
            WriteLine("ttif(!_{0}.Success)", GetHashCode());
            WriteLine("tt{");
            SetErrorMessage(errmsg);
            WriteLine("tt}");
            WriteLine("ttelse{");
            SetSuccessMessage(successmsg);
            WriteLine("tt}");
            WriteLine("t}");
            return this;
        }
        public Validator Regex(string regex, string errmsg, string successmsg)
        {
            WriteLine("tif(_{0}.Success)", GetHashCode());
            WriteLine("t{");
            WriteLine("tt_{0}.MatchRegex($('#{1}').val(),'{2}');", GetHashCode(), mControl, regex);
            WriteLine("ttif(!_{0}.Success)", GetHashCode());
            WriteLine("tt{");
            SetErrorMessage(errmsg);
            WriteLine("tt}");
            WriteLine("ttelse{");
            SetSuccessMessage(successmsg);
            WriteLine("tt}");
            WriteLine("t}");
            return this;
        }
        public Validator SelectCheckbox(int selectcount, string errmsg, string successmsg, params string[] controls)
        {
            WriteLine("tif(_{0}.Success)", GetHashCode());
            WriteLine("t{");
            WriteLine("tt_{0}.SelectCheckbox($('#{1}').val(),'{2}');", GetHashCode(), mControl, selectcount);
            WriteLine("ttif(!_{0}.Success)", GetHashCode());
            WriteLine("tt{");
            SetErrorMessage(errmsg);
            WriteLine("tt}");
            WriteLine("ttelse{");
            SetSuccessMessage(successmsg);
            WriteLine("tt}");
            WriteLine("t}");
            return this;
        }
        public Validator StringCompare(string rc, CompareType type, string errmsg, string successmsg)
        {
            WriteLine("tif(_{0}.Success)", GetHashCode());
            WriteLine("t{");
            WriteLine("tt_{0}.StringCompare($('#{1}').val(),$('#{2}').val(),'{3}');", GetHashCode(), Control,rc, type.ToString());
            WriteLine("ttif(!_{0}.Success)", GetHashCode());
            WriteLine("tt{");
            SetErrorMessage(errmsg);
            WriteLine("tt}");
            WriteLine("ttelse{");
            SetSuccessMessage(successmsg);
            WriteLine("tt}");
            WriteLine("t}");
            return this;
        }
        public Validator DateCompare(string rc, CompareType type, string errmsg, string successmsg)
        {
            WriteLine("tif(_{0}.Success)", GetHashCode());
            WriteLine("t{");
            WriteLine("tt_{0}.DateCompare($('#{1}').val(),$('#{2}').val(),'{3}');", GetHashCode(), Control, rc, type.ToString());
            WriteLine("ttif(!_{0}.Success)", GetHashCode());
            WriteLine("tt{");
            SetErrorMessage(errmsg);
            WriteLine("tt}");
            WriteLine("ttelse{");
            SetSuccessMessage(successmsg);
            WriteLine("tt}");
            WriteLine("t}");
            return this;
        }
        public Validator NumberCompare(string rc, CompareType type, string errmsg, string successmsg)
        {
            WriteLine("tif(_{0}.Success)", GetHashCode());
            WriteLine("t{");
            WriteLine("tt_{0}.NumberCompare($('#{1}').val(),$('#{2}').val(),'{3}');", GetHashCode(), Control, rc, type.ToString());
            WriteLine("ttif(!_{0}.Success)", GetHashCode());
            WriteLine("tt{");
            SetErrorMessage(errmsg);
            WriteLine("tt}");
            WriteLine("ttelse{");
            SetSuccessMessage(successmsg);
            WriteLine("tt}");
            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("tt_{0}.SelectCheckboxFromTo('{1}','{2}','{3}','{4}');", GetHashCode(), mControl, from,to,selectcount);
            WriteLine("ttif(!_{0}.Success)", GetHashCode());
            WriteLine("tt{");
            SetErrorMessage(msg);
            WriteLine("tt}");
            WriteLine("ttelse{");
            SetErrorMessage(successmsg);
            WriteLine("tt}");
            WriteLine("t}");
            return this;
        }
        public override string ToString()
        {
            return mScript.ToString()+"}rn";
        }
    }
    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("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";
        }
    }

扩展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;
}


  Code

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

本文作者:未知

本文引用网址:
在下列搜索引擎中搜索“实现一个JavaScript验证的Asp.net Helper”的相关信息:
谷歌搜索 百度搜索 雅虎搜索 搜狗搜索 搜搜搜索 必应搜索 有道搜索
你可能还喜欢以下文章
  • 1雨后小故事 qq表情姐弟雨后小故...
  • 2qq表情雨后的故事3 姐姐给弟弟...
  • 3四川美院女生遭前男友发艳照报...
  • 4实拍90后女孩拉客100元带套20分...
  • 5林晨钰爆奶门照片ed2k 林晨钰爆...
  • 1如何搭建第一个网站?做网站有...
  • 2网络红人如何赚钱?我要成为网...
  • 3说说"电视棒"为什么卖得这么火...
  • 4Discuz!不能正常发送邮件-DISC...
  • 5电子商务网站建设的重要因素点...
  • 1中国女大学生的淫靡生活
  • 2合肥艳照门事件 合肥艳照门全套...
  • 3林晨钰爆奶门视频qvod 林晨钰爆...
  • 4武汉十七中教室门种子 教室门q...
  • 5舒淇艳照门事件 舒淇不雅照ed2...
  • 1Xen和虚拟化技术学习指南
  • 2瑞星播报--“灰鸽子变种HO”病...
  • 3如何做服务器安全维护?有哪些...
  • 4硬盘数据修复软件EasyRecovery...
  • 5Windows Server 2003 安全性指...
  • 赞助商广告

    图片文章导读