using
System;
using
System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Reflection;
namespace
WebApplication1
{
public
partial
class
RegularExpressionInput : System.Web.UI.UserControl
{
///
<summary>
///
验证类型枚举
///
</summary>
public
enum
validation
{
整数,
正整数,
负整数,
数字,
电话或手机,
正数,
负数,
浮点数,
正浮点数,
负浮点数,
浮点数2,
非负浮点数,
非正浮点数,
邮件,
颜色,
url,
中文,
ACSII字符,
邮编,
手机,
IP地址,
非空,
图片,
压缩文件,
日期,
QQ号码,
国内电话,
用户名,
字母,
大写字母,
小写字母,
身份证
}
private
validation val;
///
<summary>
///
验证类型
///
</summary>
public
validation Validation
{
get
{
return
val; }
set
{ val
=
value; }
}
private
string
errorMsg;
///
<summary>
///
出错提示
///
</summary>
public
string
ErrorMsg
{
get
{
return
errorMsg; }
set
{ errorMsg
=
value; }
}
///
<summary>
///
输入文本
///
</summary>
public
string
Text
{
get
{
return
this
.TextBox1.Text; }
set
{
this
.TextBox1.Text
=
value; }
}
private
System.Drawing.Color msgColor;
///
<summary>
///
错误提示颜色
///
</summary>
public
System.Drawing.Color MsgColor
{
get
{
return
msgColor; }
set
{ msgColor
=
value; }
}
protected
string
color;
protected
string
pattern;
protected
void
Page_Load(
object
sender, EventArgs e)
{
switch
(Convert.ToInt32(val))
{
case
0
:
//
整数
pattern
=
"
/^-?[1-9]\\d*$/
"
;
break
;
case
1
:
//
正整数
pattern
=
"
/^[1-9]\\d*$/
"
;
break
;
case
2
:
//
负整数
pattern
=
"
/^-[1-9]\\d*$/
"
;
break
;
case
3
:
//
数字
pattern
=
"
/^([+-]?)\\d*\\.?\\d+$/
"
;
break
;
case
4
:
//
电话或手机
pattern
=
"
/d*\\-?\\d+$/
"
;
break
;
case
5
:
//
正数(正整数 + 0)
pattern
=
"
/^[1-9]\\d*|0$/
"
;
break
;
case
6
:
//
负数(负整数 + 0)
pattern
=
"
/^-[1-9]\\d*|0$/
"
;
break
;
case
7
:
//
浮点数
pattern
=
"
/^([+-]?)\\d*\\.\\d+$/
"
;
break
;
case
8
:
//
正浮点数
pattern
=
"
/^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$/
"
;
break
;
case
9
:
//
负浮点数
pattern
=
"
/^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$/
"
;
break
;
case
10
:
//
浮点数
pattern
=
"
/^-?([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0)$/
"
;
break
;
case
11
:
//
非负浮点数(正浮点数 + 0)
pattern
=
"
/^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$/
"
;
break
;
case
12
:
//
非正浮点数(负浮点数 + 0)
pattern
=
"
/^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$/
"
;
break
;
case
13
:
//
邮件
pattern
=
"
/^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$/
"
;
break
;
case
14
:
//
颜色
pattern
=
"
/^[a-fA-F0-9]{6}$/
"
;
break
;
case
15
:
//
url(http格式的)
pattern
=
"
/^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-.\\/?%&=]*)?$/
"
;
break
;
case
16
:
//
仅中文
pattern
=
"
/^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$/
"
;
break
;
case
17
:
//
仅ACSII字符
pattern
=
"
/^[\\x00-\\xFF]+$/
"
;
break
;
case
18
:
//
邮编
pattern
=
"
/^\\d{6}$/
"
;
break
;
case
19
:
//
手机(13号段和15号段)
pattern
=
"
/^(13|15)[0-9]{9}$/
"
;
break
;
case
20
:
//
ip地址
pattern
=
"
/^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5]).(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5]).(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5]).(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$/
"
;
break
;
case
21
:
//
非空
pattern
=
"
/^\\S+$/
"
;
break
;
case
22
:
//
图片
pattern
=
"
/(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$/
"
;
break
;
case
23
:
//
压缩文件
pattern
=
"
/(.*)\\.(rar|zip|7zip|tgz)$/
"
;
break
;
case
24
:
//
日期
pattern
=
"
/^\\d{4}(\\-|\\/|\\.)\\d{1,2}\\1\\d{1,2}$/
"
;
break
;
case
25
:
//
QQ号码
pattern
=
"
/^[1-9]*[1-9][0-9]*$/
"
;
break
;
case
26
:
//
国内电话
pattern
=
"
/(\\d{3}-|\\d{4}-)?(\\d{8}|\\d{7})/
"
;
break
;
case
27
:
//
用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串
pattern
=
"
/^(?!\\d)[a-zA-Z0-9\\u4e00-\\u9fa5]+$/
"
;
break
;
case
28
:
//
字母
pattern
=
"
/^[A-Za-z]+$/
"
;
break
;
case
29
:
//
大写字母
pattern
=
"
/^[A-Z]+$/
"
;
break
;
case
30
:
//
小写字母
pattern
=
"
/^[a-z]+$/
"
;
break
;
case
31
:
//
身份证
pattern
=
"
/^[1-9]([0-9]{14}|[0-9]{17})$/
"
;
break
;
default
:
pattern
=
string
.Empty;
break
;
}
color
=
msgColor.IsEmpty
?
System.Drawing.Color.Red.Name :
"
#
"
+
msgColor.Name.Substring(
2
);
TextBox1.Attributes[
"
onkeyup
"
]
+=
"
if(this.value.match(
"
+
pattern
+
"
)){var err=document.getElementById('
"
+
this
.ClientID
+
"
_error');err.innerHTML='';return true;}else{var err=document.getElementById('
"
+
this
.ClientID
+
"
_error');err.innerHTML=\
""
+
(
string
.IsNullOrEmpty(errorMsg)
?
"
*
"
: errorMsg)
+
"
\
"
;
return
false
;}
"
;
AddAttributes(Page);
}
///
<summary>
///
为所有具有CausesValidation属性的控件添加检查脚本
///
</summary>
///
<param name="ctrl"></param>
private
void
AddAttributes(Control ctrl)
{
if
(ctrl.HasControls())
{
foreach
(Control c
in
ctrl.Controls)
{
Type t
=
c.GetType();
PropertyInfo causeVal
=
t.GetProperty(
"
CausesValidation
"
,
typeof
(
bool
));
if
(causeVal
!=
null
)
{
PropertyInfo attr
=
t.GetProperty(
"
Attributes
"
,
typeof
(AttributeCollection));
AttributeCollection attributes
=
(AttributeCollection)attr.GetValue(c,
null
);
attributes[
"
onclick
"
]
+=
"
var txt = document.getElementById('
"
+
this
.TextBox1.ClientID
+
"
');if(txt.value.match(
"
+
pattern
+
"
)){var err=document.getElementById('
"
+
this
.ClientID
+
"
_error');err.innerHTML='';return true;}else{var err=document.getElementById('
"
+
this
.ClientID
+
"
_error');err.innerHTML=\
""
+
(
string
.IsNullOrEmpty(errorMsg)
?
"
*
"
: errorMsg)
+
"
\
"
;
return
false
;}
"
;
}
AddAttributes(c);
}
}
}
}
}