zoukankan
html css js c++ java
一段输入框控制代码,包含所有控制条件!
//
LastTime
1.3
Edition
-----------------------------
#
region 版本说明 edition explain
/*
*
1.2
版本
*
------------------------------------------------------------------------------
*
1
>
新加入 是否必须验证
,
默认是不必须验证的,设计 IsRequest 为true 后就为必须验证了
*
使用方法
:
小数带参数
|
@abc
<
3
-
2
>
*
控件名前加入 @ 即可
*
*
2
>
日期验证 DateTime 类别 命令
=
日期
|
控件名
*
*
ERROR
:
出现了 限制输入验证时不正确的情况
*
Date
:
2005
-
4
-
29
*
1
>
DateTime 的限制输入时什么都输入不了
*
2
>
Url 等等在限制输入时一定也有这种情况
*
3
>
限制输入只能用于检测 输入是 数字还是 字符 的情况。
*
ErrorModify
:
应把这类的 验证都识别为 [All]类型的验证
*
*
1.2
.
1
*
---------------------------------------------------------------------------------
*
新加入DateTime的另一个控件,可以选择分的控件
*
*
1.3
2005
-
5
-
4
*
---------------------------------------------------------------------------------
*
对控件加入服务器端的验证
*
可以防止在JAVASCRIPT出现错误或失效后,影响程序的正常运行!
*
用validate() 来验证, 运行是一个原理利用 Regex 来验证,但是服务器端的
.
新加入一个新的函数即可
.
*
*
新问题:
*
-------------
*
发现在小数限制时当小于2
<
2
,
3
>
时, 验证为
-
1晕
*
*/
#
endregion
#region 版权说明 copyright explain
/*
*
Create By Liangzhimy
*
任何单位或个人都可以更改并更新本版本,但更新后的版本请发送到mailto
:|
@abc
<
3
-
2中确认。
*
任何单位或个人都可以分发此验证版本,但请保留版权信息
*/
#
endregion
#region 说明
/*
*
create by liangzhimy
*
2005
-
4
-
20
*
*
用于验证的类
*
可以扩展的内容有
*
1
可以加入验证的js的位置
*
2
未加入日期验证
*
string commandtext
=
"
不为空|TextBox1\\小数后留两位|TextBox2,TextBox3,TextBox5,TextBox6,TextBox7,TextBox8,TextBox9,TextBox10,TextBox11,dl,cl\\整型|TextBox4
"
;
*
*
新的需求,要求限制输入的长度, 如果是小数点的话可以限制输入的 小数位
*
如
*
*
先设计一个属性, 表示都使用两种验证
*
不为空
|
TextBox1
<
4
>\\
小数后留两位
|
TextBox2
<
4
,
2
>
*
*
关系到几个正则
*
1
限制长度
^
[A
-
Za
-
z
\
u0391
-\
uFFE5
\
d]{
length
}$
*
2
小数限制,整个长度,及其小数位数
*
*
限制长度
*
1
>
^
[A
-
Za
-
z
\
u0391
-\
uFFE5
\
d]{
length
}$
*
2
>
^\
w{
length
}$
*
*
*
*
小数限制 整个长度,及其小数位数
length
整个长度 decimallen 小数位长度
*
^
(
0
|
[
1
-
9
]
\\
d{
0
,
3
})(file
://.//
d{
0
,
2
})
?
$
*
[restrict]
*
注 当是[all] 类型的验证时 新添加类别时要在相应的js里也添加相就相应的类别
'
*
* 不为空|TextBox1<4>\\小数后留两位|TextBox2<4-2-3-4-5>
* 小数带参数|abc<3-2>,cc<4-5>\\限制长度|l<3>,b<3>,c<4>,d<5>
*
* 问题:中文输入时的问题, 在验证时无法进行正确的验证 已经解决, 为预留的类型 LmitB
*
*
*
*/
#endregion
using System;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Text.RegularExpressions;
namespace PDWY.Tools
{
#region 枚举类别
/// <summary>
/// 两种验证的方法, ALL提交时验证 Restrict输入时验证
/// </summary>
public enum Type
{
/// <summary>
/// 提交时验证
/// </summary>
All,
/// <summary>
/// 输入时验证
/// </summary>
Restrict,
/// <summary>
/// 两种验证方式都可以
/// </summary>
ANY
}
/// <summary>
/// 验证的类别
/// </summary>
public enum CheckDataType
{ /// <summary>
/// 不验证
/// </summary>
None,
/// <summary>
/// 不为空
/// </summary>
Require,
/// <summary>
/// 电子邮件
/// </summary>
Email,
/// <summary>
/// 电话
/// </summary>
Phone,
/// <summary>
/// 手机
/// </summary>
Mobile,
/// <summary>
/// URL
/// </summary>
Url,
/// <summary>
/// 信用卡
/// </summary>
IdCard,
/// <summary>
/// Currency
/// </summary>
Currency,
/// <summary>
/// 数字类型
/// </summary>
Number,
/// <summary>
/// 邮编类型
/// </summary>
Zip,
/// <summary>
/// QQ
/// </summary>
QQ,
/// <summary>
/// 整型
/// </summary>
Integer,
/// <summary>
/// 整形能大于0
/// </summary>
IntergerTo0,
/// <summary>
/// 4位数的整型
/// </summary>
Integer4,
/// <summary>
/// 浮点
/// </summary>
Double,
/// <summary>
/// 浮点
/// </summary>
Float,
/// <summary>
/// 后面带两个小数点的浮点
/// </summary>
Float2,
/// <summary>
/// 英文
/// </summary>
English,
/// <summary>
/// 中文
/// </summary>
Chinese,
/// <summary>
/// 不安全的输入
/// </summary>
UnSafe,
/// <summary>
/// 自定义长度限制 length
/// </summary>
CustomLength,
/// <summary>
/// 限制浮点与范围值 length|decimallen
/// </summary>
CustomFloat,
/// <summary>
/// 验证为日期格式
/// </summary>
DateTime,
CustomFloat1
}
#endregion
#region 后定义的类别工厂
#region 自定义的类别的抽象类
/// <summary>
/// 自定义的类别的抽象类
/// </summary>
public abstract class Custom
{
#region 属性
private string _controlAttribute="";
/// <summary>
/// 控件的属性
/// </summary>
public string ControlAttribute
{
get
{
return _controlAttribute;
}
set
{
_controlAttribute=value;
}
}
private IList _Param;
/// <summary>
/// 参数值
/// </summary>
public IList Param
{
get
{
return _Param;
}
set
{
_Param=value;
}
}
private IList _OutTagParam;
/// <summary>
/// 标记属性
/// </summary>
public IList OutTageParam
{
get
{
return _OutTagParam;
}
set
{
_OutTagParam=value;
}
}
private Type _CType=Type.ANY;
/// <summary>
/// [all|abrast|any]这个类别可以适用于哪几个类别的操作
/// </summary>
public Type CType
{
get
{
return _CType;
}
set
{
_CType=value;
}
}
private string _script="";
/// <summary>
/// 这个验证可以包含的操作[这是一个可行的项目,可以自定义其中的函数]
/// </summary>
public string Script
{ get
{
return _script;
}
set
{
_script=value;
}
}
// private string _ScriptRegName;
/// <summary>
/// 表示 被 注册到系统中的JAVASCRIT块的名称 , 如果是同一个类别的是不是可以是同一个块的名称呢, 当然可以了,
/// 那就把它设成只读的,嘿嘿!
/// </summary>
public string ScriptRegName
{
get
{
return "script" + _ControlType.ToString();
}
}
private string _Message="";
/// <summary>
/// 提示给用户的信息
/// </summary>
public string Message
{
get
{
return _Message;
}
set
{
_Message=value;
}
}
protected CheckDataType _ControlType;
/// <summary>
/// 操作的类别
/// </summary>
public CheckDataType ControlType
{
get
{
return _ControlType;
}
}
private bool _isRequir=false;
/// <summary>
/// 标识为是否必须验证
/// </summary>
public bool IsRequir
{
get
{
return _isRequir;
}
set
{
_isRequir=value;
}
}
public virtual void AddAttribute(System.Web.UI.WebControls.WebControl control)
{
return;
}
#endregion
public abstract string GetRegex();
}
#endregion
#region 限制长度的抽象类
/// <summary>
/// 限制长度的抽象类
/// </summary>
public class CustomLength:Custom
{
public CustomLength()
{
}
public CustomLength(IList l)
{
if(l!=null)
{
this.Param=l;
this.OutTageParam=new ArrayList();
this.OutTageParam.Add("MaxLength");
//---------------------------------
//添加验证长度
this.OutTageParam.Add("dataType");
this.OutTageParam.Add("min");
this.OutTageParam.Add("max");
this.Param.Add("LimitB");
this.Param.Add("1");
this.Param.Add(l[0].ToString());
//---------------------------------
// this.Message="限制长度" + l[0].ToString() + "位之内,中文是两位字符!";
this.Message="长度不正确!";
}
_ControlType=CheckDataType.CustomLength;
}
/// <summary>
/// 自定义的添加验证事件
/// </summary>
private void CustomScript()
{
if(this.Param.Count==this.OutTageParam.Count)
{
// string script="";
}
}
public override string GetRegex()
{
string mailto:|@abc<3-2";
if(Param!=null)
{
regexstring=regexstring.Replace("length",this.Param[0].ToString());
return regexstring;
}
return null;
}
}
#endregion
#region 限制浮点与范围值|小数带参数 abc<3-2>,cc<4-5>
public class CustomFloat:Custom
{
public override void AddAttribute(System.Web.UI.WebControls.WebControl control)
{
// control.Attributes.Add("Style1","IME-MODE:disabled");
control.Attributes.Add("style","IME-MODE:disabled");
// base.AddAttribute (control);
}
public CustomFloat(){}
public void cLength(IList l)
{
if(l!=null)
{
//---------------------------------
//添加验证长度
this.OutTageParam.Add("dataType");
this.Param.Add(CheckDataType.Float.ToString());
}
}
public CustomFloat(IList l)
{ this.Param=l;
this.OutTageParam=new ArrayList();
//添加两个和参数相对应的属性
this.OutTageParam.Add("length");
this.OutTageParam.Add("decimallen");
// this.ControlAttribute="style=\"IME-MODE:disabled\"";
// this.OutTageParam.Add("style");
// this.Param.Add("IME-MODE:disabled");
if(l!=null)
// this.Message="限制浮点与范围值:长度" + l[0].ToString() + "位;小数位" + l[1].ToString() + "位";
this.Message="格式不正确!请输入小数!";
this.cLength(l);
// CType=Type.Restrict; //说明这个类只能用于输入验证
_ControlType=CheckDataType.CustomFloat;
}
public override string GetRegex()
{
// string regexstring="^[-\\+]?(0|[1-9]\\d{0,length})(file://.//d{0,decimallen})?$";
//在验证时不得以为每一个要验证的数据后加入限制符{0,1} | ?
string regexstring="^((0|[1-9])\\d{0,length})?(file://.//d{0,decimallen})?$";
if(this.Param!=null)
{
//新加入的//更改一下,因为在这个系统中传入的length为已经减入过后的值了
//******************************************************
//只针对这个系统 ,用过这个系统后可以改回来
// int tmpleng=int.Parse(this.Param[0].ToString())-int.Parse(this.Param[1].ToString())-1;
int tmpleng=int.Parse(this.Param[0].ToString())-1;
//******************************************************
if(tmpleng==-1)
{
regexstring="^(0)(file://.//d{0,decimallen})?$";
regexstring=regexstring.Replace("decimallen",this.Param[1].ToString());
}
else
{
if(tmpleng<0)
tmpleng=int.Parse(this.Param[0].ToString());
//regexstring=regexstring.Replace("length",this.Param[0].ToString());
regexstring=regexstring.Replace("length",tmpleng.ToString());
regexstring=regexstring.Replace("decimallen",this.Param[1].ToString());
//更新当精度 和 小数位 相同 时, 只允许输入0.111111*
}
}
return regexstring;
}
}
#endregion
#region 限制浮点与范围值|小数带参数 abc<3-2>,cc<4-5>
public class CustomFloat1:Custom
{
public CustomFloat1(){}
public override void AddAttribute(System.Web.UI.WebControls.WebControl control)
{
control.Attributes.Add("style","IME-MODE:disabled");
}
public void cLength(IList l)
{
if(l!=null)
{
//---------------------------------
//添加验证长度
this.OutTageParam.Add("dataType");
this.Param.Add(CheckDataType.Float.ToString());
}
}
public CustomFloat1(IList l)
{
this.Param=l;
this.OutTageParam=new ArrayList();
//添加两个和参数相对应的属性
this.OutTageParam.Add("length");
this.OutTageParam.Add("decimallen");
if(l!=null)
// this.Message="限制浮点与范围值:长度" + l[0].ToString() + "位;小数位" + l[1].ToString() + "位";
this.Message="格式不正确!请输入小数!";
this.cLength(l);
// CType=Type.Restrict; //说明这个类只能用于输入验证
_ControlType=CheckDataType.CustomFloat;
}
public override string GetRegex()
{
// string regexstring="^[-\\+]?(0|[1-9]\\d{0,length})(file://.//d{0,decimallen})?$";
//在验证时不得以为每一个要验证的数据后加入限制符{0,1} | ?
string regexstring="^[-\\+]?((0|[1-9])\\d{0,length})?(file://.//d{0,decimallen})?$";
if(this.Param!=null)
{
//新加入的//更改一下,因为在这个系统中传入的length为已经减入过后的值了
//******************************************************
//只针对这个系统 ,用过这个系统后可以改回来
// int tmpleng=int.Parse(this.Param[0].ToString())-int.Parse(this.Param[1].ToString())-1;
int tmpleng=int.Parse(this.Param[0].ToString())-1;
//******************************************************
if(tmpleng==-1)
{
regexstring="^(0)(file://.//d{0,decimallen})?$";
regexstring=regexstring.Replace("decimallen",this.Param[1].ToString());
}
else
{
if(tmpleng<0)
tmpleng=int.Parse(this.Param[0].ToString());
//regexstring=regexstring.Replace("length",this.Param[0].ToString());
regexstring=regexstring.Replace("length",tmpleng.ToString());
regexstring=regexstring.Replace("decimallen",this.Param[1].ToString());
//更新当精度 和 小数位 相同 时, 只允许输入0.111111*
}
}
return regexstring;
}
}
#endregion
#region 用来适应其它类型
public class Common:Custom
{
#region 返回相应类别的验证的正则表达式
/// <summary>
/// 返回相应类别的验证的正则表达式
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
private string getRestrictRegex(CheckDataType t)
{
string re="";
switch(t)
{
case CheckDataType.Float2:
re="^(0|[1-9]\\d{0,3})?(file://.//d{0,2})?$";
break;
case CheckDataType.Integer4:
re="^(0|[1-9]\\d{0,3})?$";
break;
case CheckDataType.Require:
re=".+";
break;
case CheckDataType.Email:
re="^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
break;
case CheckDataType.Phone:
//对于限制输入 来说{0,3} 开头必须是0
// re="^((file://(//d{3}//))|(//d{3}//-))?(//(0//d{2,3}//)|0//d{2,3}-)?[1-9]//d{6,7}$";
//修改后的
re="^((file://(//d{3}//))|(//d{3}//-))?(//(0//d{0,3}//)|0//d{0,3}-)?[1-9]//d{0,7}$";
break;
case CheckDataType.Mobile:
re="^((file://(//d{3}//))|(//d{3}//-))?13//d{9}$";
break;
case CheckDataType.Url:
re="^http:\\/\\/[A-Za-z0-9]+\\.[A-Za-z0-9]+[\\/=\\?%\\-&_~`@[\\]\\
'
:+!
]
*
([
^<>
"
;
re +=
"
\\\
"
\\\
"
])
*
$
"
;
break;
case CheckDataType.IdCard:
re=
"
^\\
d{
15
}(file
://
d{
2
}[A
-
Za
-
z0
-
9
])
?
$
/
"
;
break;
case CheckDataType.Currency:
re=
"
^\\
d
+
(file
://.//
d
+
)
?
$
"
;
break;
case CheckDataType.Number:
re=
"
^\\
d
+
$
"
;
break;
case CheckDataType.Zip:
re=
"
^
[
1
-
9
]
\\
d{
5
}$
"
;
break;
case CheckDataType.QQ:
//re=
"
^
[
1
-
9
]
\\
d{
4
,
8
}$
"
;
re=
"
^
[
1
-
9
]
\\
d{
0
,
8
}$
"
;
break;
case CheckDataType.Integer:
re=
"
^
[
-\\+
]
?\\
d{
0
,
10
}$
"
;
break;
case CheckDataType.IntergerTo0:
re=
"
^\\
d{
0
,
10
}$
"
;
break;
case CheckDataType.Double:
re=
"
^
[
-\\+
]
?\\
d
+
(file
://.//
d
+
)
?
$
"
;
break;
case CheckDataType.Float:
// re=
"
^
[
-\\+
]
?\\
d
+
(file
://.//
d
+
)
?
$
"
;
re=
"
^
[
-\\+
]
?
(file
://
d)
*
(
//.//
d
+
)
?
$
"
;
break;
case CheckDataType.English:
re=
"
^
[A
-
Za
-
z]
+
$
"
;
break;
case CheckDataType.Chinese:
re=
"
^
[
\\
u0391
-\\
uFFE5]
+
$
"
;
break;
case CheckDataType.UnSafe:
re=
"
^
(([A
-
Z]
*|
[a
-
z]
*|\\
d
*|
[
-
_
\\~!
@
#
\\$%\\^&\\*\\.\\(file://)//[//]//{//}<>//?/////////'///"]*)|.{0,5})$|//s";
break;
case CheckDataType
.
DateTime
:
//
re
=
"
^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$
"
;
//
re
=
"
^((((1[6-9]|[2-9]\\d?)\\d{0,2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{0,2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{0,2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$
"
;
mailto
:|
@abc
<
3
-
2
"
; //注自己写的,格式如:2005-1-1
break;
}
return re;
}
#endregion
#region 返回相应的类别的提示信息
private string getDefaultMsg(CheckDataType t)
{
string re=
""
;
// string splitstr=
"
|
"
;
switch(t)
{
case CheckDataType.Float2:
re=
"
需输入两位小数位的浮点值
"
;
break;
case CheckDataType.Integer4:
re=
"
需输入4位的整数
"
;
break;
case CheckDataType.Require:
re=
"
不能为空
"
;
break;
case CheckDataType.Email:
re=
"
需输入Emalil 如 mailto
:|
@abc
<
3
-
2
"
;
break;
case CheckDataType.Phone:
re=
"
需输入电话格式
"
;
break;
case CheckDataType.Mobile:
re=
"
需输入手机格式
"
;
break;
case CheckDataType.Url:
re=
"
需输入网址 如 http
://
www
.
aaa
.
com
/
"
;
break;
case CheckDataType.IdCard:
re=
"
需输入卡号格式
"
;
break;
case CheckDataType.Currency:
re=
"
需输入Currency格式
"
;
break;
case CheckDataType.Number:
re=
"
需输入数字
"
;
break;
case CheckDataType.Zip:
re=
"
需输入邮编格式 如:
100000
"
;
break;
case CheckDataType.QQ:
re=
"
需输入QQ
"
;
break;
case CheckDataType.Integer:
re=
"
需输入整数型
"
;
break;
case CheckDataType.Double:
re=
"
需输入双精度浮点型
"
;
break;
case CheckDataType.Float:
re=
"
需输入浮点型
"
;
break;
case CheckDataType.English:
re=
"
需输入英文
"
;
break;
case CheckDataType.Chinese:
re=
"
需输入中文
"
;
break;
case CheckDataType.UnSafe:
re=
"
不安全代码
"
;
break;
case CheckDataType.DateTime:
re=
"
日期格式不正确
,
正确的格式信息如
1999
-
1
-
1
"
;
break;
}
re= re;
return re;
}
#endregion
// public Common(){ }
private string regexstr=
""
;
public Common(CheckDataType t)
{
this.Message=this.getDefaultMsg(t);
regexstr=this.getRestrictRegex(t);
this._ControlType=t;
if(t==CheckDataType.Require)
this.IsRequir=true;
switch(t)
{
case CheckDataType.Float2:
this.CType=Type.ANY;
break;
case CheckDataType.Integer4:
this.CType=Type.ANY;
break;
case CheckDataType.Number:
this.CType=Type.ANY;
break;
case CheckDataType.Integer:
this.CType=Type.ANY;
break;
case CheckDataType.Double:
this.CType=Type.ANY;
break;
case CheckDataType.Float:
this.CType=Type.ANY;
break;
case CheckDataType.English:
this.CType=Type.ANY;
break;
case CheckDataType.Chinese:
this.CType=Type.ANY;
break;
}
}
public override string GetRegex()
{
return this.regexstr;
}
}
#endregion
#region 工厂类返回指定的类别
public class CustomFactory
{
public static Custom GetCustom(CheckDataType t,IList p)
{ Custom item;
switch(t)
{
case CheckDataType.CustomLength:
item=new CustomLength(p);
break;
case CheckDataType.CustomFloat:
item=new CustomFloat(p);
break;
case CheckDataType.CustomFloat1:
item=new CustomFloat1(p);
break;
default:
item=new Common(t);
break;
}
item.Message=
"
|
"
+ item.Message;
return item;
}
}
#endregion
#endregion
/// <summary>
/// 页面上的控件进行限制
/// </summary>
public class WebFormValidate
{
#region 属性
public enum ALLCheckType
{
/// <summary>
/// 第一种验证方式
/// </summary>
one,
/// <summary>
/// 第二种验证方式
/// </summary>
two,
/// <summary>
/// 第三种验证方式
/// </summary>
Three
}
private string _ValidateJsPath=
"
http://www.cnblogs.com/
js
/
validateJs
.
js
"
;
/// <summary>
/// 验证类的Js文件的存放位置
/// </summary>
public string ValidateJsPath
{
get
{
return _ValidateJsPath;
}
set
{
_ValidateJsPath=value;
}
}
private ALLCheckType _CheckTypeWhereALL=ALLCheckType.one;
/// <summary>
/// 当使用ALL验证时的验证的方式
/// </summary>
public WebFormValidate.ALLCheckType CheckTypeWhereALL
{
get
{
return _CheckTypeWhereALL;
}
set
{
_CheckTypeWhereALL=value;
}
}
private string _DateCalenderJsPath=
"
http://www.cnblogs.com/
js
/
PopupCalendar
.
js
"
;
/// <summary>
/// 日期控件Js文件的存放放置
/// </summary>
public string DateCalenderJsPath
{
get
{
return _DateCalenderJsPath;
}
set
{
_DateCalenderJsPath=value;
}
}
// private string _scriptString=
"
<
script language
=\
"
javascript\
"
src
=\
"
/js/validateJs.js\
"
>
</
script
>
"
;
protected string scriptString
{
get
{
return
"
<
script language
=\
"
javascript\
"
src
=\
""
+
this
.
_ValidateJsPath
+
"
\
"
charset
=\
"
gb2312\
"
>
</
script
>
"
;
}
}
//private string _DateTimeCalJsPath=
"
../
javascript
/
time
.
htm
"
;
private string _DateTimeCalJsPath=
"
http://www.cnblogs.com/
js
/
time
.
htm
"
;
public string DateTimeCalJsPath
{
get
{
return _DateTimeCalJsPath;
}
set
{ _DateTimeCalJsPath=value;
}
}
#endregion
#region 构造函数
private Type _checkType=Type.All;
public Type CheckType
{
set
{
_checkType=value;
}
get
{
return _checkType;
}
}
public WebFormValidate()
{
scriptRestrict +=
"
<
script
>
"
;
scriptRestrict +=
"
function regInput(obj
,
reg
,
inputStr)
"
+ System.Environment.NewLine;
scriptRestrict +=
"
{ var docSel
=
document
.
selection
.
createRange()
"
+ System.Environment.NewLine;
scriptRestrict +=
"
oSel
=
docSel
.
duplicate()
"
+ System.Environment.NewLine;
scriptRestrict +=
"
oSel
.
text
=
\
"
\
"
"
+ System.Environment.NewLine;
scriptRestrict +=
"
var srcRange
=
obj
.
createTextRange()
"
+ System.Environment.NewLine;
scriptRestrict +=
"
oSel
.
setEndPoint(
\
"
StartToStart\
"
,
srcRange)
"
+ System.Environment.NewLine;
scriptRestrict +=
"
var str
=
oSel
.
text
+
inputStr
+
srcRange
.
text
.
substr
(oSel
.
text
.
length
)
"
+ System.Environment.NewLine;
scriptRestrict +=
"
return
reg
.
test(str)
"
+ System.Environment.NewLine;
scriptRestrict +=
"
}
</
script
>
"
+ System.Environment.NewLine;
}
// public WebFormValidate(Type t)
// {
// _checkType=t;
// scriptRestrict +=
"
<
script
>
"
;
// scriptRestrict +=
"
function regInput(obj
,
reg
,
inputStr)
"
;
// scriptRestrict +=
"
{ var docSel
=
document
.
selection
.
createRange()
"
;
// scriptRestrict +=
"
oSel
=
docSel
.
duplicate()
"
;
// scriptRestrict +=
"
oSel
.
text
=
\
"
\
"
"
;
// scriptRestrict +=
"
var srcRange
=
obj
.
createTextRange()
"
;
// scriptRestrict +=
"
oSel
.
setEndPoint(
\
"
StartToStart\
"
,
srcRange)
"
;
// scriptRestrict +=
"
var str
=
oSel
.
text
+
inputStr
+
srcRange
.
text
.
substr
(oSel
.
text
.
length
)
"
;
// scriptRestrict +=
"
return
reg
.
test(str)
"
;
// scriptRestrict +=
"
}
</
script
>
"
;
// }
#endregion
#region other
public void initAllControl(Control control)
{
}
private string scriptRestrict=
""
;
/// <summary>
/// 返回相应类别的验证的正则表达式
/// </summary>
/// <param name=
"
t
"
></param>
/// <returns></returns>
private string getRestrictRegex(CheckDataType t)
{
string re=
""
;
switch(t)
{
case CheckDataType.Float2:
re=
"
^
(
0
|
[
1
-
9
]
\\
d{
0
,
3
})(file
://.//
d{
0
,
2
})
?
$
"
;
break;
case CheckDataType.Integer4:
re=
"
^
(
0
|
[
1
-
9
]
\\
d{
0
,
3
})
?
$
"
;
break;
case CheckDataType.Require:
re=
"
.+
"
;
break;
case CheckDataType.Email:
re=
"
^\\
w
+
([
-+.
]
\\
w
+
)
*
@
\\
w
+
([
-.
]
\\
w
+
)
*\\.\\
w
+
([
-.
]
\\
w
+
)
*
$
"
;
break;
case CheckDataType.Phone:
re=
"
^
((file
://
(
//
d{
3
}
//
))
|
(
//
d{
3
}
//-
))
?
(
//
(
0
//
d{
2
,
3
}
//
)
|
0
//
d{
2
,
3
}
-
)
?
[
1
-
9
]
//
d{
6
,
7
}$
"
;
break;
case CheckDataType.Mobile:
re=
"
^
((file
://
(
//
d{
3
}
//
))
|
(
//
d{
3
}
//-
))
?
13
//
d{
9
}$
"
;
break;
case CheckDataType.Url:
re=
"
^
http
:\\/\\/
[A
-
Za
-
z0
-
9
]
+\\.
[A
-
Za
-
z0
-
9
]
+
[
\\/=\\?%\\-&
_
~
`@[
\\
]
\\
'
:+!]*([^<>";
re +="\\\"\\\"])*$";
break;
case CheckDataType.IdCard:
re="^\\d{15}(file://d{2}[A-Za-z0-9])?$/";
break;
case CheckDataType.Currency:
re="^\\d+(file://.//d+)?$";
break;
case CheckDataType.Number:
re="^\\d+$";
break;
case CheckDataType.Zip:
re="^[1-9]\\d{5}$";
break;
case CheckDataType.QQ:
re="^[1-9]\\d{4,8}$";
break;
case CheckDataType.Integer:
//对整形验证加入长度验证10位
//re="^[-\\+]?\\d+$";
re="^[-\\+]?\\d{10}$";
break;
case CheckDataType.Double:
re="^[-\\+]?\\d+(file://.//d+)?$";
break;
case CheckDataType.Float:
re="^[-\\+]?\\d+(file://.//d+)?$";
break;
case CheckDataType.English:
re="^[A-Za-z]+$";
break;
case CheckDataType.Chinese:
re="^[\\u0391-\\uFFE5]+$";
break;
case CheckDataType.UnSafe:
re="^(([A-Z]*|[a-z]*|\\d*|[-_\\~!@#\\$%\\^&\\*\\.\\(file://)//[//]//{//}<>//?/////////
'
///
"
]*)|.{0,5})$|//s
"
;
break;
}
return
re;
}
#
endregion
#region 操作函数
public void addValidate(
System
.
Web
.
UI
.
WebControls
.
WebControl bu
,
string formName)
{
if
((bu is
System
.
Web
.
UI
.
WebControls
.
Button)
||
(bu is
System
.
Web
.
UI
.
WebControls
.
ImageButton)
||
(bu is
System
.
Web
.
UI
.
WebControls
.
LinkButton))
{
switch(CheckTypeWhereALL)
{
case ALLCheckType
.
one
:
bu
.
Attributes
.
Add(
"
onClick
"
,
"
return Validator.Validate(document.getElementById('
"
+
formName
+
"
'))
"
);
break;
case ALLCheckType
.
two
:
bu
.
Attributes
.
Add(
"
onClick
"
,
"
return Validator.Validate(document.getElementById('
"
+
formName
+
"
'),2)
"
);
break;
case ALLCheckType
.
Three
:
bu
.
Attributes
.
Add(
"
onClick
"
,
"
return alidator.Validate(document.getElementById('
"
+
formName
+
"
'),3)
"
);
break;
}
}
}
///
<
summary
>
///
当用户没有传入MESSAGE值的时候给MESSAGE附初始值
///
</
summary
>
///
<
param name
=
"
mess
"
></
param
>
///
<
returns
></
returns
>
private string getDefaultMsg(CheckDataType t)
{
string re
=
""
;
switch(t)
{
case CheckDataType
.
Float2
:
re
=
"
需输入两位小数位的浮点值
"
;
break;
case CheckDataType
.
Integer4
:
re
=
"
需输入4位的整数
"
;
break;
case CheckDataType
.
Require
:
re
=
"
不能为空
"
;
break;
case CheckDataType
.
Email
:
re
=
"
需输入Emalil 如 mailto:|@abc<3-2
"
;
break;
case CheckDataType
.
Phone
:
re
=
"
需输入电话格式
"
;
break;
case CheckDataType
.
Mobile
:
re
=
"
需输入手机格式
"
;
break;
case CheckDataType
.
Url
:
re
=
"
需输入网址 如 http://www.aaa.com/
"
;
break;
case CheckDataType
.
IdCard
:
re
=
"
需输入卡号格式
"
;
break;
case CheckDataType
.
Currency
:
re
=
"
需输入Currency格式
"
;
break;
case CheckDataType
.
Number
:
re
=
"
需输入数字
"
;
break;
case CheckDataType
.
Zip
:
re
=
"
需输入邮编格式 如: 100000
"
;
break;
case CheckDataType
.
QQ
:
re
=
"
需输入QQ
"
;
break;
case CheckDataType
.
Integer
:
re
=
"
需输入整数型
"
;
break;
case CheckDataType
.
Double
:
re
=
"
需输入双精度浮点型
"
;
break;
case CheckDataType
.
Float
:
re
=
"
需输入浮点型
"
;
break;
case CheckDataType
.
English
:
re
=
"
需输入英文
"
;
break;
case CheckDataType
.
Chinese
:
re
=
"
需输入中文
"
;
break;
case CheckDataType
.
UnSafe
:
re
=
"
不安全代码
"
;
break;
case CheckDataType
.
DateTime
:
re
=
"
日期的格式不正确
"
;
break;
}
return
re;
}
///
<
summary
>
///
全部参数验证
///
</
summary
>
///
<
param name
=
"
page
"
></
param
>
///
<
param name
=
"
control
"
></
param
>
///
<
param name
=
"
lx
"
></
param
>
///
<
param name
=
"
type
"
></
param
>
///
<
param name
=
"
msg
"
></
param
>
public void addControlToValidate(Page page
,
System
.
Web
.
UI
.
WebControls
.
WebControl control
,
Type lx
,
CheckDataType type
,
string msg)
{
if
(lx
==
Type
.
All
||
lx
==
Type
.
ANY)
{
//
string scriptString
=
"
<script language=\
"
javascript
\
"
src=\
"
/
js
/
validateJs
.
js
\
"
> </script>
"
;
if
(
!
page
.
IsClientScriptBlockRegistered(
"
clientScript
"
))
page
.
RegisterClientScriptBlock(
"
clientScript
"
,
scriptString);
control
.
Attributes
.
Add(
"
dataType
"
,
type
.
ToString());
//---------
重新给mes值
if
(msg
==
""
)
{
msg
=
this
.
getDefaultMsg(type);
}
control
.
Attributes
.
Add(
"
msg
"
,
msg);
//
isHaveTypeAll
=
true;
}
else
{
if
(
!
page
.
IsClientScriptBlockRegistered(
"
clientScript1
"
)
&&
scriptRestrict
!=
""
)
page
.
RegisterClientScriptBlock(
"
clientScript1
"
,
scriptRestrict);
string tmp
=
this
.
getRestrictRegex(type);
if
(tmp
!=
""
)
{
string checktmp
=
@
"
return regInput(this,/
"
+
this
.
getRestrictRegex(type)
+
"
/,String.fromCharCode(event.keyCode))
"
;
string parsetmp
=
@
"
return regInput(this,/
"
+
this
.
getRestrictRegex(type)
+
"
/,window.clipboardData.getData('Text'))
"
;
control
.
Attributes
.
Add(
"
onkeypress
"
,
checktmp);
//
按键事件加入判断
control
.
Attributes
.
Add(
"
onpaste
"
,
parsetmp);
//
粘贴事件
//
control
.
Attributes
.
Add(
"
ime-mode
"
,
"
Disabled
"
);
//
关闭中文输入法
control
.
Attributes
.
Add(
"
ondrag
"
,
"
return false;
"
);
//
关闭拖拽
}
//
isHaveTypeRestrict
=
true;
}
}
///
<
summary
>
///
带有参数的全部操作
///
</
summary
>
///
<
param name
=
"
page
"
></
param
>
///
<
param name
=
"
control
"
></
param
>
///
<
param name
=
"
lx
"
></
param
>
///
<
param name
=
"
type
"
></
param
>
///
<
param name
=
"
msg
"
></
param
>
///
<
param name
=
"
param
"
></
param
>
public void addControlToValidate(Page page
,
System
.
Web
.
UI
.
WebControls
.
WebControl control
,
Type lx
,
CheckDataType type
,
string msg
,
IList param
,
bool isRequir)
{
Custom item
=
PDWY
.
Tools
.
CustomFactory
.
GetCustom(type
,
param);
if
(isRequir)
item
.
IsRequir
=
true;
//---------
重新给mes值
if
(msg
!=
""
)
{
item
.
Message
=
msg
+
item
.
Message;
}
item
.
AddAttribute(control);
//
如果这个验证可以适应所调用的验证的话就调用
switch(lx)
{ case Type
.
ANY
:
addControlToValidate(page
,
control
,
item);
addControlToValidate(page
,
control
,
item
,
""
);
break;
case Type
.
All
:
addControlToValidate(page
,
control
,
item);
break;
case Type
.
Restrict
:
addControlToValidate(page
,
control
,
item
,
""
);
break;
}
}
//
新加入的内容
public void RegScript(Page page
,
System
.
Web
.
UI
.
WebControls
.
WebControl control
,
Custom item)
{
//
注册验证的自定义的验证函数
//------------------------------
if
(item
.
Script
!=
""
)
{
if
(
!
page
.
IsClientScriptBlockRegistered(item
.
ScriptRegName))
page
.
RegisterClientScriptBlock(item
.
ScriptRegName
,
item
.
Script);
}
//
将自定义的属性事件加入的客户端
if
(item
.
OutTageParam
!=
null)
{
if
(item
.
OutTageParam
.
Count
>
0
)
{
int
i
=
0
;
foreach
(string tmpitem in item
.
OutTageParam)
{
if
(item
.
Param[i]
!=
null)
{
//
如果已经有自定义添加的属性则删除这个属性后再添加
if
(control
.
Attributes[tmpitem]
!=
null)
{
control
.
Attributes
.
Remove(tmpitem);
}
control
.
Attributes
.
Add(tmpitem
,
item
.
Param[i]
.
ToString());
}
i
++
;
}
}
}
//--------------------------------------
}
///
<
summary
>
///
[ALL类型验证][新加入了带参数的类别]将验证加入到验证列表里,最后还要调用全部验证
///
</
summary
>
///
<
param name
=
"
page
"
></
param
>
///
<
param name
=
"
control
"
></
param
>
///
<
param name
=
"
type
"
></
param
>
///
<
param name
=
"
msg
"
></
param
>
public void addControlToValidate(Page page
,
System
.
Web
.
UI
.
WebControls
.
WebControl control
,
Custom item)
{
if
(item
.
CType
==
Type
.
ANY
||
item
.
CType
==
Type
.
All)
{
if
(
!
page
.
IsClientScriptBlockRegistered(
"
clientScript
"
))
page
.
RegisterClientScriptBlock(
"
clientScript
"
,
scriptString);
//
在默认情况下使所有的验证都为不必填的
require
=
"
false
"
CheckDataType
.
Require是个例外
if
(
!
item
.
IsRequir)
control
.
Attributes
.
Add(
"
require
"
,
"
false
"
);
else
{
if
(control
.
Attributes[
"
require
"
]
!=
null)
{
if
(control
.
Attributes[
"
require
"
]
!=
"
true
"
)
{
control
.
Attributes[
"
require
"
]
=
"
true
"
;
}
}
}
//-----------------------------------------
control
.
Attributes
.
Add(
"
dataType
"
,
item
.
ControlType
.
ToString());
//
注册验证的自定义的验证函数
//------------------------------
this
.
RegScript(page
,
control
,
item);
//--------------------------------------
//
System
.
Text
.
Encoding
.
ASCII
.
control
.
Attributes
.
Add(
"
msg
"
,
item
.
Message);
}
}
///
<
summary
>
///
[限制类别验证][新加入了带参数的类别]直接限制输入
///
</
summary
>
///
<
param name
=
"
page
"
></
param
>
///
<
param name
=
"
control
"
></
param
>
///
<
param name
=
"
item
"
></
param
>
public void addControlToValidate(Page page
,
System
.
Web
.
UI
.
WebControls
.
WebControl control
,
Custom item
,
string l)
{
if
(item
.
CType
==
Type
.
ANY
||
item
.
CType
==
Type
.
Restrict)
{
if
(
!
page
.
IsClientScriptBlockRegistered(
"
clientScript1
"
)
&&
scriptRestrict
!=
""
)
page
.
RegisterClientScriptBlock(
"
clientScript1
"
,
scriptRestrict);
string tmp
=
item
.
GetRegex();
if
(tmp
!=
""
)
{
string checktmp
=
@
"
return regInput(this,/
"
+
item
.
GetRegex()
+
"
/,String.fromCharCode(event.keyCode))
"
;
string parsetmp
=
@
"
return regInput(this,/
"
+
item
.
GetRegex()
+
"
/,window.clipboardData.getData('Text'))
"
;
control
.
Attributes
.
Add(
"
onkeypress
"
,
checktmp);
//
按键事件加入判断
control
.
Attributes
.
Add(
"
onpaste
"
,
parsetmp);
//
粘贴事件
//
control
.
Attributes
.
Add(
"
ime-mode
"
,
"
Disabled
"
);
//
关闭中文输入法
control
.
Attributes
.
Add(
"
ondrag
"
,
"
return false;
"
);
//
关闭拖拽
}
//
注册验证的自定义的验证函数
//------------------------------
this
.
RegScript(page
,
control
,
item);
//--------------------------------------
}
}
///
<
summary
>
///
[ALL类型验证]将验证加入到验证列表里,最后还要调用全部验证
///
</
summary
>
///
<
param name
=
"
page
"
></
param
>
///
<
param name
=
"
control
"
></
param
>
///
<
param name
=
"
type
"
></
param
>
///
<
param name
=
"
msg
"
></
param
>
public void addControlToValidate(Page page
,
System
.
Web
.
UI
.
WebControls
.
WebControl control
,
CheckDataType type
,
string msg)
{
if
(
!
page
.
IsClientScriptBlockRegistered(
"
clientScript
"
))
page
.
RegisterClientScriptBlock(
"
clientScript
"
,
scriptString);
control
.
Attributes
.
Add(
"
dataType
"
,
type
.
ToString());
//
System
.
Text
.
Encoding
.
ASCII
.
control
.
Attributes
.
Add(
"
msg
"
,
msg);
}
///
<
summary
>
///
[限制类别验证]直接限制输入
///
</
summary
>
///
<
param name
=
"
page
"
></
param
>
///
<
param name
=
"
control
"
></
param
>
///
<
param name
=
"
type
"
></
param
>
public void addControlToValidate(Page page
,
System
.
Web
.
UI
.
WebControls
.
WebControl control
,
CheckDataType type)
{
if
(
!
page
.
IsClientScriptBlockRegistered(
"
clientScript1
"
)
&&
scriptRestrict
!=
""
)
page
.
RegisterClientScriptBlock(
"
clientScript1
"
,
scriptRestrict);
string tmp
=
this
.
getRestrictRegex(type);
if
(tmp
!=
""
)
{ string checktmp
=
@
"
return regInput(this,/
"
+
this
.
getRestrictRegex(type)
+
"
/,String.fromCharCode(event.keyCode))
"
;
string parsetmp
=
@
"
return regInput(this,/
"
+
this
.
getRestrictRegex(type)
+
"
/,window.clipboardData.getData('Text'))
"
;
control
.
Attributes
.
Add(
"
onkeypress
"
,
checktmp);
//
按键事件加入判断
control
.
Attributes
.
Add(
"
onpaste
"
,
parsetmp);
//
粘贴事件
//
control
.
Attributes
.
Add(
"
ime-mode
"
,
"
Disabled
"
);
//
关闭中文输入法
control
.
Attributes
.
Add(
"
ondrag
"
,
"
return false;
"
);
//
关闭拖拽
}
}
#
endregion
#region 简化操作的函数
//
这是命令的格式
:
不为空
|
as[all](信息)
,
bc
,
c
,
d
,
e
\
整型
|
ab
,
cc
,
dd
\
小数后留两位
|
dd
,
ee
,
ff
//
private string _controlstring
=
""
;
//
public string ControlString{ get{
return
_controlstring; } set{ _controlstring
=
value; } }
///
<
summary
>
///
简化操作的函数 [这是命令的格式
:
不为空
|
as[all](信息)
,
bc
,
c
,
d
,
e
\
整型
|
ab
,
cc
,
dd
\
小数后留两位
|
dd
,
ee
,
ff]
///
</
summary
>
///
<
param name
=
"
page
"
></
param
>
///
<
param name
=
"
bu
"
></
param
>
///
<
param name
=
"
cstring
"
></
param
>
///
<
param name
=
"
formName
"
></
param
>
public void addValidate(Page page
,
System
.
Web
.
UI
.
WebControls
.
WebControl bu
,
string cstring
,
string formName)
{
char first
=
'
\\
'
;
char second
=
'
|
'
;
char three
=
'
,
'
;
bool isall
=
false;
//
先分开不同的验证类别
string[] typestring
=
cstring
.
Split
(first);
foreach
(string firstTmp in typestring)
{
//
再分开验证的类别与控件
string[] typeAndcontrol
=
firstTmp
.
Split
(second);
//
排除不是这个验证字符串的内容
if
(typeAndcontrol
.
Length
==
2
)
{
string commandTmp
=
typeAndcontrol[
0
];
string controlTmp
=
typeAndcontrol[
1
];
string[] controls
=
controlTmp
.
Split
(three);
//
这时controls是取到的控件名称
foreach
(string controlName1 in controls)
{
string controlName
=
controlName1;
//
默认类别
string type
=
"
[any]
"
;
string typeany
=
"
[any]
"
;
string typeall
=
"
[all]
"
;
string typeRestrict
=
"
[restrict]
"
;
if
(controlName
.
IndexOf(typeall)
>=
0
)
{
controlName
=
controlName
.
Replace(typeall
,
string
.
Empty);
type
=
typeall;
}
if
(controlName
.
IndexOf(typeRestrict)
>=
0
)
{
controlName
=
controlName
.
Replace(typeRestrict
,
string
.
Empty);
type
=
typeRestrict;
//
if
(isall)isall
=
false;
}
if
(controlName
.
IndexOf(typeany)
>=
0
)
{
controlName
=
controlName
.
Replace(typeany
,
string
.
Empty);
type
=
typeany;
}
if
(type
==
typeall
||
type
==
typeany)
{
isall
=
true;
}
string msg
=
""
;
//
现在查信息
controlName
=
controlName
.
Trim();
msg
=
getMessage(
ref
controlName
,
"
(
"
,
"
)
"
);
//
现在查参数 param
string param
=
getMessage(
ref
controlName
,
"
<
"
,
"
>
"
);
//
参数如
4
-
5
-
6
-
7
-
8
-
9
IList ParamList
=
new ArrayList();
string[] tmpparam
=
param
.
Split
(
'
-
'
);
foreach
(string tmpparamitem in tmpparam)
{
if
(tmpparamitem
!=
""
)
ParamList
.
Add(tmpparamitem);
}
//
ParamList 为空则表示没有参数
bool isRequir
=
false;
//
新加入
@命令表示为必须验证
if
(controlName
.
IndexOf(
"
@
"
,
0
)
==
0
)
{
//
表示必须验证
isRequir
=
true;
controlName
=
controlName
.
Remove(
0
,
1
);
}
//
现在有三个数 type 表示用哪一种验证方式
|
controlName 表示要被操作的操作
|
commandTmp 表示要具体是哪一种操作
if
(commandTmp
==
""
||
type
==
""
||
controlName
==
""
)
{
continue
;}
else
{
Control c
=
page
.
FindControl(controlName);
if
(c is
System
.
Web
.
UI
.
WebControls
.
WebControl)
{
Type mtype;
type
=
type
.
Trim();
if
(type
==
typeRestrict) mtype
=
Type
.
Restrict;
else
{
if
(type
==
typeany) mtype
=
Type
.
ANY;
else
mtype
=
Type
.
All;
}
commandTmp
=
commandTmp
.
Trim();
if
(ParamList
.
Count
!=
0
)
this
.
addControlToValidate(page
,
(
System
.
Web
.
UI
.
WebControls
.
WebControl)c
,
mtype
,
getDataType(commandTmp)
,
msg
,
ParamList
,
isRequir);
else
this
.
addControlToValidate(page
,
(
System
.
Web
.
UI
.
WebControls
.
WebControl)c
,
mtype
,
getDataType(commandTmp)
,
msg
,
null
,
isRequir);
}
}
}
}
}
if
(isall)this
.
addValidate(bu
,
formName);
}
///
<
summary
>
///
当命令行以以下格式 传入时 不为空
|
as[all](信息) ,可以 选择出 “信息” 来
///
</
summary
>
///
<
param name
=
"
s
"
></
param
>
///
<
param name
=
"
up
"
></
param
>
///
<
param name
=
"
down
"
></
param
>
///
<
returns
></
returns
>
private string getMessage(
ref
string s
,
string up
,
string down)
{string re
=
s;
if
(s
.
IndexOf(up)
>
0
&&
s
.
IndexOf(down)
>
0
)
{
re
=
s
.
Substring(s
.
IndexOf(up)
,
s
.
Length
-
s
.
IndexOf(up));
re
=
re
.
Replace(up
,
string
.
Empty);
re
=
re
.
Replace(down
,
string
.
Empty);
s
=
s
.
Replace(up
+
re
+
down
,
string
.
Empty);
}
else
{
re
=
""
;
}
return
re;
}
private CheckDataType getDataType(string type)
{
switch(type)
{
case
"
不为空
"
:
return
CheckDataType
.
Require
;
case
"
整型
"
:
return
CheckDataType
.
Integer;
case
"
正整型
"
:
return
CheckDataType
.
IntergerTo0;
case
"
小数后留两位
"
:
return
CheckDataType
.
Float2;
case
"
小数带参数
"
:
return
CheckDataType
.
CustomFloat;
case
"
小数带参数可为负
"
:
return
CheckDataType
.
CustomFloat1;
case
"
限制长度
"
:
return
CheckDataType
.
CustomLength;
case
"
电子邮件
"
:
return
CheckDataType
.
Email;
case
"
电话
"
:
return
CheckDataType
.
Phone;
case
"
手机
"
:
return
CheckDataType
.
Mobile;
case
"
URL
"
:
return
CheckDataType
.
Url;
case
"
信用卡
"
:
return
CheckDataType
.
IdCard;
case
"
数字类型
"
:
return
CheckDataType
.
Number;
case
"
邮编类型
"
:
return
CheckDataType
.
Zip;
case
"
QQ
"
:
return
CheckDataType
.
QQ;
case
"
4位数的整型
"
:
return
CheckDataType
.
Integer4;
case
"
双精度浮点
"
:
return
CheckDataType
.
Double;
case
"
浮点
"
:
return
CheckDataType
.
Float;
case
"
英文
"
:
return
CheckDataType
.
English;
case
"
中文
"
:
return
CheckDataType
.
Chinese;
case
"
不安全的输入
"
:
return
CheckDataType
.
UnSafe;
case
"
日期
"
:
return
CheckDataType
.
DateTime;
}
return
CheckDataType
.
None;
}
#
endregion
#region 服务器端验证
#region 进行验证的函数
///
<
summary
>
///
服务器端应当都为必须验证的
///
</
summary
>
///
<
param name
=
"
control
"
></
param
>
///
<
param name
=
"
item
"
></
param
>
///
<
returns
></
returns
>
public bool IsValidateOnServer(
System
.
Web
.
UI
.
WebControls
.
WebControl control
,
Custom item)
{
bool re
=
true;
if
(control is
System
.
Web
.
UI
.
WebControls
.
TextBox)
{
System
.
Web
.
UI
.
WebControls
.
TextBox textbox
=
(
System
.
Web
.
UI
.
WebControls
.
TextBox)control;
string tmpresult
=
textbox
.
Text;
string regex
=
item
.
GetRegex();
re
=
regexCheck(regex
,
tmpresult);
}
return
re;
}
private bool regexCheck(string regex
,
string result)
{
//
bool re
=
false;
return
Regex
.
IsMatch(result
,
regex);
//
return
re;
}
#
endregion
#region 简化操作的函数
//
这是命令的格式
:
不为空
|
as[all](信息)
,
bc
,
c
,
d
,
e
\
整型
|
ab
,
cc
,
dd
\
小数后留两位
|
dd
,
ee
,
ff
//
private string _controlstring
=
""
;
//
public string ControlString{ get{
return
_controlstring; } set{ _controlstring
=
value; } }
///
<
summary
>
///
简化操作的函数 [这是命令的格式
:
不为空
|
as[all](信息)
,
bc
,
c
,
d
,
e
\
整型
|
ab
,
cc
,
dd
\
小数后留两位
|
dd
,
ee
,
ff]
///
</
summary
>
///
<
param name
=
"
cstring
"
></
param
>
public bool addValidateOnServer(Page page
,
string cstring)
{
bool re
=
true;
//
当前需要有的操作是 分析命令字、生成Item 、与直接验证
char first
=
'
\\
'
;
char second
=
'
|
'
;
char three
=
'
,
'
;
bool isall
=
false;
//
先分开不同的验证类别
string[] typestring
=
cstring
.
Split
(first);
foreach
(string firstTmp in typestring)
{
//
再分开验证的类别与控件
string[] typeAndcontrol
=
firstTmp
.
Split
(second);
//
排除不是这个验证字符串的内容
if
(typeAndcontrol
.
Length
==
2
)
{
string commandTmp
=
typeAndcontrol[
0
];
string controlTmp
=
typeAndcontrol[
1
];
string[] controls
=
controlTmp
.
Split
(three);
//
这时controls是取到的控件名称
foreach
(string controlName1 in controls)
{
string controlName
=
controlName1;
//
默认类别
string type
=
"
[any]
"
;
string typeany
=
"
[any]
"
;
string typeall
=
"
[all]
"
;
string typeRestrict
=
"
[restrict]
"
;
if
(controlName
.
IndexOf(typeall)
>=
0
)
{
controlName
=
controlName
.
Replace(typeall
,
string
.
Empty);
type
=
typeall;
}
if
(controlName
.
IndexOf(typeRestrict)
>=
0
)
{
controlName
=
controlName
.
Replace(typeRestrict
,
string
.
Empty);
type
=
typeRestrict;
//
if
(isall)isall
=
false;
}
if
(controlName
.
IndexOf(typeany)
>=
0
)
{
controlName
=
controlName
.
Replace(typeany
,
string
.
Empty);
type
=
typeany;
}
if
(type
==
typeall
||
type
==
typeany)
{
isall
=
true;
}
string msg
=
""
;
//
现在查信息
controlName
=
controlName
.
Trim();
msg
=
getMessage(
ref
controlName
,
"
(
"
,
"
)
"
);
//
现在查参数 param
string param
=
getMessage(
ref
controlName
,
"
<
"
,
"
>
"
);
//
参数如
4
-
5
-
6
-
7
-
8
-
9
IList ParamList
=
new ArrayList();
string[] tmpparam
=
param
.
Split
(
'
-
'
);
foreach
(string tmpparamitem in tmpparam)
{
if
(tmpparamitem
!=
""
)
ParamList
.
Add(tmpparamitem);
}
//
ParamList 为空则表示没有参数
bool isRequir
=
false;
//
新加入
@命令表示为必须验证
if
(controlName
.
IndexOf(
"
@
"
,
0
)
==
0
)
{
//
表示必须验证
isRequir
=
true;
controlName
=
controlName
.
Remove(
0
,
1
);
}
//
现在有三个数 type 表示用哪一种验证方式
|
controlName 表示要被操作的操作
|
commandTmp 表示要具体是哪一种操作
if
(commandTmp
==
""
||
type
==
""
||
controlName
==
""
)
{
continue
;}
else
{
Control c
=
page
.
FindControl(controlName);
if
(c is
System
.
Web
.
UI
.
WebControls
.
WebControl)
{
Type mtype;
type
=
type
.
Trim();
if
(type
==
typeRestrict) mtype
=
Type
.
Restrict;
else
{
if
(type
==
typeany) mtype
=
Type
.
ANY;
else
mtype
=
Type
.
All;
}
commandTmp
=
commandTmp
.
Trim();
if
(isRequir)
{
Custom item
=
CustomFactory
.
GetCustom(getDataType(commandTmp)
,
ParamList);
if
(msg
!=
""
)
{
item
.
Message
=
msg;
}
//
如果服务器没有验证成功
if
(
!
this
.
IsValidateOnServer((
System
.
Web
.
UI
.
WebControls
.
WebControl)c
,
item))
{
//
输出相应的错误信息了
,
然后返回一个有错误的提示
re
=
false;
break;
}
}
}
}
}
}
}
return
re;
}
#
endregion
#endregion
#region 日期控件
protected string dateCalstring
{
get
{
return
"
<script language=\
"
javascript
\
"
src=\
""
+ _DateCalenderJsPath +
"
\
"
charset=\
"
gb2312
\
"
></script>
"
;
}
}
public void addDateCal(Page page
,
System
.
Web
.
UI
.
WebControls
.
WebControl control)
{
//
string datejs
=
"
<script>
"
+
System
.
Environment
.
NewLine;
//
datejs
+=
"
var oCalendarChs=new PopupCalendar(\
"
oCalendarChs
\
"
); //初始化控件时,请给出实例名称:oCalendarChs
"
+
System
.
Environment
.
NewLine;
//
datejs
+=
"
oCalendarChs.weekDaySting=new Array(\
"
日
\
"
,\
"
一
\
"
,\
"
二
\
"
,\
"
三
\
"
,\
"
四
\
"
,\
"
五
\
"
,\
"
六
\
"
);
"
;
//
datejs
+=
"
oCalendarChs.monthSting=new Array(\
"
一月
\
"
,\
"
二月
\
"
,\
"
三月
\
"
,\
"
四月
\
"
,\
"
五月
\
"
,\
"
六月
\
"
,\
"
七月
\
"
,\
"
八月
\
"
,\
"
九月
\
"
,\
"
十月
\
"
,\
"
十一月
\
"
,\
"
十二月
\
"
);
"
+
System
.
Environment
.
NewLine;
//
datejs
+=
"
oCalendarChs.oBtnTodayTitle=\
"
今天
\
"
;
"
+
System
.
Environment
.
NewLine;
//
datejs
+=
"
oCalendarChs.oBtnClearTitle=\
"
清空
\
"
;
"
+
System
.
Environment
.
NewLine;
//
datejs
+=
"
oCalendarChs.oBtnCancelTitle=\
"
取消
\
"
;
"
+
System
.
Environment
.
NewLine;
//
datejs
+=
"
oCalendarChs.Init();
"
+
System
.
Environment
.
NewLine;
//
datejs
+=
"
</script>
"
+
System
.
Environment
.
NewLine;
//
if
(
!
page
.
IsClientScriptBlockRegistered(
"
clientScript2
"
)
&&
dateCalstring
!=
""
)
//
page
.
RegisterClientScriptBlock(
"
clientScript2
"
,
dateCalstring);
//
//
if
(
!
page
.
IsClientScriptBlockRegistered(
"
clientScript3
"
)
&&
datejs
!=
""
)
//
page
.
RegisterClientScriptBlock(
"
clientScript3
"
,
datejs);
//
//
control
.
Attributes
.
Add(
"
onclick
"
,
"
getDateString(this,oCalendarChs)
"
);
string datejs
=
"
<script language=\
"
javascript
\
"
src=\
"
http://www.cnblogs.com/
js
/
setday
.
js
\
"
charset=\
"
gb2312
\
"
></script>
"
;
//
if
(
!
page
.
IsClientScriptBlockRegistered(
"
clientScript2
"
)
&&
dateCalstring
!=
""
)
//
page
.
RegisterClientScriptBlock(
"
clientScript2
"
,
dateCalstring);
if
(
!
page
.
IsClientScriptBlockRegistered(
"
clientScript3
"
)
&&
datejs
!=
""
)
page
.
RegisterClientScriptBlock(
"
clientScript3
"
,
datejs);
control
.
Attributes
.
Add(
"
onclick
"
,
"
setday(this);return false;
"
);
}
///
<
summary
>
///
加入日期控件 可以给日期控件加入只读属性
///
</
summary
>
///
<
param name
=
"
page
"
></
param
>
///
<
param name
=
"
control
"
></
param
>
///
<
param name
=
"
strReadOnly
"
>
是否只读的属性
</
param
>
public void addDateCal(Page page
,
System
.
Web
.
UI
.
WebControls
.
WebControl control
,
bool strReadOnly)
{
this
.
addDateCal(page
,
control);
if
(strReadOnly)
control
.
Attributes
.
Add(
"
readonly
"
,
"
readonly
"
);
}
///
<
summary
>
///
加入日期控件带有时间的选择
///
</
summary
>
///
<
param name
=
"
page
"
></
param
>
///
<
param name
=
"
control
"
></
param
>
public void addDateTimeCal(Page page
,
System
.
Web
.
UI
.
WebControls
.
WebControl control)
{
string temp
=
""
;
temp
+=
"
<SCRIPT>
"
+
System
.
Environment
.
NewLine;
temp
+=
"
function SelDateTime(obj)
"
+
System
.
Environment
.
NewLine;
temp
+=
"
{
"
+
System
.
Environment
.
NewLine;
temp
+=
"
var para=\
"
scroll
:
no
;status
:
no
;center
:
yes;unadorned
:
no
;dialogHeight
:
215px;dialogWidth
:
450px;
\
""
+
System
.
Environment
.
NewLine;
//
temp
+=
"
window.showModalDialog(\
"
time
.
htm
?\
"
+Math.random(),obj, para)
"
+
System
.
Environment
.
NewLine;
temp
+=
"
window.showModalDialog(\
""
+ _DateTimeCalJsPath +
"
?\
"
+Math.random(),obj, para)
"
+
System
.
Environment
.
NewLine;
temp
+=
"
}
"
+
System
.
Environment
.
NewLine;
temp
+=
"
"
+
System
.
Environment
.
NewLine;
temp
+=
"
function regInput(obj, reg, inputStr)
"
+
System
.
Environment
.
NewLine;
temp
+=
"
{
"
+
System
.
Environment
.
NewLine;
temp
+=
"
var docSel = document.selection.createRange()
"
+
System
.
Environment
.
NewLine;
temp
+=
"
oSel = docSel.duplicate()
"
+
System
.
Environment
.
NewLine;
temp
+=
"
oSel.text = \
"
\
""
+
System
.
Environment
.
NewLine;
temp
+=
"
var srcRange = obj.createTextRange()
"
+
System
.
Environment
.
NewLine;
temp
+=
"
oSel.setEndPoint(\
"
StartToStart
\
"
, srcRange)
"
+
System
.
Environment
.
NewLine;
temp
+=
"
var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length)
"
+
System
.
Environment
.
NewLine;
temp
+=
"
return reg.test(str)
"
+
System
.
Environment
.
NewLine;
temp
+=
"
}
"
+
System
.
Environment
.
NewLine;
temp
+=
"
"
+
System
.
Environment
.
NewLine;
temp
+=
"
</SCRIPT>
"
+
System
.
Environment
.
NewLine;
if
(
!
page
.
IsClientScriptBlockRegistered(
"
pdwydateTimeCal
"
))
page
.
RegisterClientScriptBlock(
"
pdwydateTimeCal
"
,
temp);
control
.
Attributes
.
Add(
"
onclick
"
,
"
SelDateTime(this);
"
);
}
///
<
summary
>
///
加入日期控件带有时间的选择 并可以选择时间
///
</
summary
>
///
<
param name
=
"
page
"
></
param
>
///
<
param name
=
"
control
"
></
param
>
///
<
param name
=
"
strReadOnly
"
>
true 为只读, false 为需要只读
</
param
>
public void addDateTimeCal(Page page
,
System
.
Web
.
UI
.
WebControls
.
WebControl control
,
bool strReadOnly)
{
this
.
addDateTimeCal(page
,
control);
if
(strReadOnly)
control
.
Attributes
.
Add(
"
readonly
"
,
"
readonly
"
);
}
#
endregion
}
}
查看全文
相关阅读:
学习笔记:松弛
学习笔记:可持久化线段树
poj 3784 Running Median
学习笔记:树状数组
poj 2823 Sliding Window 题解
学习笔记:状态压缩DP
学习笔记:单调队列
C++ 竞赛常用头文件
mongodb lock 出毛病时解决方法
ag使用需要注意的问题
原文地址:https://www.cnblogs.com/bingyun84/p/1605277.html
最新文章
poj 3041 Asteroids 题解
vijos p1729 Knights
bzoj 1975 [Sdoi2010]魔法猪学院
洛谷P1772 [ZJOI2006]物流运输 题解
学习笔记:斐波那契堆
蛇梯棋(转载)
POJ 1270 Following Orders
学习笔记:欧拉函数
bzoj 1565 [NOI2009]植物大战僵尸 解题报告
poj 1861 Network 解题报告
热门文章
poj 2585 Window Pains 解题报告
poj 2284 That Nice Euler Circuit 解题报告
bzoj 3670 [NOI2014]动物园
tyvj P2018 「Nescafé26」小猫爬山 解题报告
poj 1201 Intervals 解题报告
uva 10618 Tango Tango Insurrection 解题报告
uva 1025 A Spy in the Metro 解题报告
C++ cout 格式化输出方法
学习笔记:AC自动机
学习笔记:A*算法
Copyright © 2011-2022 走看看