实现一个JavaScript验证的Asp.net Helper
2009-09-29 00:00:00 来源:WEB开发网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
}
Tags:实现 一个 JavaScript
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接