当前位置: 首页 > news >正文

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

 

       WEB应用在HTML里写JavaScript(JS)验证数据是正常的事情,但VS.NETJS的智能感知支持远没有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功能代码

ContractedBlock.gif ExpandedBlockStart.gif Code
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代码

ContractedBlock.gif ExpandedBlockStart.gif Code
    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代码


ContractedBlock.gif ExpandedBlockStart.gif Code
 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功能

ContractedBlock.gif ExpandedBlockStart.gif Code
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;
}

 

ContractedBlock.gif ExpandedBlockStart.gif Code
        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;
        }



 

相关文章:

  • HTML重构与网页常用工具
  • 国学应用大师翟鸿燊经典语录
  • shell---scp远程传输文件不需要手动输入密码
  • STP生成树协议
  • WDatePicker
  • OSPF笔记-2
  • 使用goldengate交付指定时间前的数据
  • 采购杀毒软件,你说话能算数么?
  • OpenStack 部署运维实战
  • 我的项目管理之路
  • jQuery - AJAX load() 方法
  • editplus与正则替换
  • Android PopupWindow 的使用
  • 两个条件求和
  • 菜鸟学exchange之二:如何管理邮件用户以及邮件策略的设置
  • (十五)java多线程之并发集合ArrayBlockingQueue
  • 【跃迁之路】【699天】程序员高效学习方法论探索系列(实验阶段456-2019.1.19)...
  • 2017-09-12 前端日报
  • 2018天猫双11|这就是阿里云!不止有新技术,更有温暖的社会力量
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • AWS实战 - 利用IAM对S3做访问控制
  • Git 使用集
  • HTTP那些事
  • PHP 的 SAPI 是个什么东西
  • Vue.js-Day01
  • 解决iview多表头动态更改列元素发生的错误
  • 微信小程序--------语音识别(前端自己也能玩)
  • 我是如何设计 Upload 上传组件的
  • 消息队列系列二(IOT中消息队列的应用)
  • - 转 Ext2.0 form使用实例
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • ​插件化DPI在商用WIFI中的价值
  • #HarmonyOS:基础语法
  • #Linux(make工具和makefile文件以及makefile语法)
  • (01)ORB-SLAM2源码无死角解析-(66) BA优化(g2o)→闭环线程:Optimizer::GlobalBundleAdjustemnt→全局优化
  • (07)Hive——窗口函数详解
  • (13)Latex:基于ΤΕΧ的自动排版系统——写论文必备
  • (6)添加vue-cookie
  • (C语言)fgets与fputs函数详解
  • (JS基础)String 类型
  • (MATLAB)第五章-矩阵运算
  • (编译到47%失败)to be deleted
  • (非本人原创)史记·柴静列传(r4笔记第65天)
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • (十八)SpringBoot之发送QQ邮件
  • (顺序)容器的好伴侣 --- 容器适配器
  • (转)h264中avc和flv数据的解析
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .NET Core 成都线下面基会拉开序幕
  • .Net Winform开发笔记(一)
  • .NET 除了用 Task 之外,如何自己写一个可以 await 的对象?
  • .net 微服务 服务保护 自动重试 Polly
  • .NET/C# 使用 SpanT 为字符串处理提升性能