NET-页面数据校验通用类
页面数据校验通用类


1
public class PageValidate
2
{
3
private static Regex RegPhone = new Regex("^[0-9]+[-]?[0-9]+[-]?[0-9]$");
4
private static Regex RegNumber = new Regex("^[0-9]+$");
5
private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
6
private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
7
private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
8
private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
9
private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
10
11
public PageValidate()
12
{
13
}
14
15
16
数字字符串检查#region 数字字符串检查
17
public static bool IsPhone(string inputData)
18
{
19
Match m = RegPhone.Match(inputData);
20
return m.Success;
21
}
22
/**//// <summary>
23
/// 检查Request查询字符串的键值,是否是数字,最大长度限制
24
/// </summary>
25
/// <param name="req">Request</param>
26
/// <param name="inputKey">Request的键值</param>
27
/// <param name="maxLen">最大长度</param>
28
/// <returns>返回Request查询字符串</returns>
29
public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen)
30
{
31
string retVal = string.Empty;
32
if(inputKey != null && inputKey != string.Empty)
33
{
34
retVal = req.QueryString[inputKey];
35
if(null == retVal)
36
retVal = req.Form[inputKey];
37
if(null != retVal)
38
{
39
retVal = SqlText(retVal, maxLen);
40
if(!IsNumber(retVal))
41
retVal = string.Empty;
42
}
43
}
44
if(retVal == null)
45
retVal = string.Empty;
46
return retVal;
47
}
48
/**//// <summary>
49
/// 是否数字字符串
50
/// </summary>
51
/// <param name="inputData">输入字符串</param>
52
/// <returns></returns>
53
public static bool IsNumber(string inputData)
54
{
55
Match m = RegNumber.Match(inputData);
56
return m.Success;
57
}
58
59
/**//// <summary>
60
/// 是否数字字符串 可带正负号
61
/// </summary>
62
/// <param name="inputData">输入字符串</param>
63
/// <returns></returns>
64
public static bool IsNumberSign(string inputData)
65
{
66
Match m = RegNumberSign.Match(inputData);
67
return m.Success;
68
}
69
/**//// <summary>
70
/// 是否是浮点数
71
/// </summary>
72
/// <param name="inputData">输入字符串</param>
73
/// <returns></returns>
74
public static bool IsDecimal(string inputData)
75
{
76
Match m = RegDecimal.Match(inputData);
77
return m.Success;
78
}
79
/**//// <summary>
80
/// 是否是浮点数 可带正负号
81
/// </summary>
82
/// <param name="inputData">输入字符串</param>
83
/// <returns></returns>
84
public static bool IsDecimalSign(string inputData)
85
{
86
Match m = RegDecimalSign.Match(inputData);
87
return m.Success;
88
}
89
90
#endregion
91
92
中文检测#region 中文检测
93
94
/**//// <summary>
95
/// 检测是否有中文字符
96
/// </summary>
97
/// <param name="inputData"></param>
98
/// <returns></returns>
99
public static bool IsHasCHZN(string inputData)
100
{
101
Match m = RegCHZN.Match(inputData);
102
return m.Success;
103
}
104
105
#endregion
106
107
邮件地址#region 邮件地址
108
/**//// <summary>
109
/// 是否是浮点数 可带正负号
110
/// </summary>
111
/// <param name="inputData">输入字符串</param>
112
/// <returns></returns>
113
public static bool IsEmail(string inputData)
114
{
115
Match m = RegEmail.Match(inputData);
116
return m.Success;
117
}
118
119
#endregion
120
121
其他#region 其他
122
123
/**//// <summary>
124
/// 检查字符串最大长度,返回指定长度的串
125
/// </summary>
126
/// <param name="sqlInput">输入字符串</param>
127
/// <param name="maxLength">最大长度</param>
128
/// <returns></returns>
129
public static string SqlText(string sqlInput, int maxLength)
130
{
131
if(sqlInput != null && sqlInput != string.Empty)
132
{
133
sqlInput = sqlInput.Trim();
134
if(sqlInput.Length > maxLength)//按最大长度截取字符串
135
sqlInput = sqlInput.Substring(0, maxLength);
136
}
137
return sqlInput;
138
}
139
/**//// <summary>
140
/// 字符串编码
141
/// </summary>
142
/// <param name="inputData"></param>
143
/// <returns></returns>
144
public static string HtmlEncode(string inputData)
145
{
146
return HttpUtility.HtmlEncode(inputData);
147
}
148
/**//// <summary>
149
/// 设置Label显示Encode的字符串
150
/// </summary>
151
/// <param name="lbl"></param>
152
/// <param name="txtInput"></param>
153
public static void SetLabel(Label lbl, string txtInput)
154
{
155
lbl.Text = HtmlEncode(txtInput);
156
}
157
public static void SetLabel(Label lbl, object inputObj)
158
{
159
SetLabel(lbl, inputObj.ToString());
160
}
161
//字符串清理
162
public static string InputText(string inputString, int maxLength)
163
{
164
StringBuilder retVal = new StringBuilder();
165
166
// 检查是否为空
167
if ((inputString != null) && (inputString != String.Empty))
168
{
169
inputString = inputString.Trim();
170
171
//检查长度
172
if (inputString.Length > maxLength)
173
inputString = inputString.Substring(0, maxLength);
174
175
//替换危险字符
176
for (int i = 0; i < inputString.Length; i++)
177
{
178
switch (inputString[i])
179
{
180
case '"':
181
retVal.Append(""");
182
break;
183
case '<':
184
retVal.Append("<");
185
break;
186
case '>':
187
retVal.Append(">");
188
break;
189
default:
190
retVal.Append(inputString[i]);
191
break;
192
}
193
}
194
retVal.Replace("'", " ");// 替换单引号
195
}
196
return retVal.ToString();
197
198
}
199
/**//// <summary>
200
/// 转换成 HTML code
201
/// </summary>
202
/// <param name="str">string</param>
203
/// <returns>string</returns>
204
public static string Encode(string str)
205
{
206
str = str.Replace("&","&");
207
str = str.Replace("'","''");
208
str = str.Replace("\"",""");
209
str = str.Replace(" "," ");
210
str = str.Replace("<","<");
211
str = str.Replace(">",">");
212
str = str.Replace("\n","<br>");
213
return str;
214
}
215
/**//// <summary>
216
///解析html成 普通文本
217
/// </summary>
218
/// <param name="str">string</param>
219
/// <returns>string</returns>
220
public static string Decode(string str)
221
{
222
str = str.Replace("<br>","\n");
223
str = str.Replace(">",">");
224
str = str.Replace("<","<");
225
str = str.Replace(" "," ");
226
str = str.Replace(""","\"");
227
return str;
228
}
229
230
public static string SqlTextClear(string sqlText)
231
{
232
if (sqlText == null)
233
{
234
return null;
235
}
236
if (sqlText == "")
237
{
238
return "";
239
}
240
sqlText = sqlText.Replace(",", "");//去除,
241
sqlText = sqlText.Replace("<", "");//去除<
242
sqlText = sqlText.Replace(">", "");//去除>
243
sqlText = sqlText.Replace("--", "");//去除--
244
sqlText = sqlText.Replace("'", "");//去除'
245
sqlText = sqlText.Replace("\"", "");//去除"
246
sqlText = sqlText.Replace("=", "");//去除=
247
sqlText = sqlText.Replace("%", "");//去除%
248
sqlText = sqlText.Replace(" ", "");//去除空格
249
return sqlText;
250
}
251
#endregion
252
253
是否由特定字符组成#region 是否由特定字符组成
254
public static bool isContainSameChar(string strInput)
255
{
256
string charInput = string.Empty;
257
if (!string.IsNullOrEmpty(strInput))
258
{
259
charInput = strInput.Substring(0, 1);
260
}
261
return isContainSameChar(strInput, charInput, strInput.Length);
262
}
263
264
public static bool isContainSameChar(string strInput, string charInput, int lenInput)
265
{
266
if (string.IsNullOrEmpty(charInput))
267
{
268
return false;
269
}
270
else
271
{
272
Regex RegNumber = new Regex(string.Format("^([{0}])+$", charInput));
273
//Regex RegNumber = new Regex(string.Format("^([{0}]{{1}})+$", charInput,lenInput));
274
Match m = RegNumber.Match(strInput);
275
return m.Success;
276
}
277
}
278
#endregion
279
280
检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查#region 检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查
281
/**//// <summary>
282
/// 检查输入的参数是不是某些定义好的特殊字符:这个方法目前用于密码输入的安全检查
283
/// </summary>
284
public static bool isContainSpecChar(string strInput)
285
{
286
string[] list = new string[]
{ "123456", "654321" };
287
bool result = new bool();
288
for (int i = 0; i < list.Length; i++)
289
{
290
if (strInput == list[i])
291
{
292
result = true;
293
break;
294
}
295
}
296
return result;
297
}
298
#endregion
299
}
300

2



3

4

5

6

7

8

9

10

11

12



13

14

15

16


17

18



19

20

21

22


23

24

25

26

27

28

29

30



31

32

33



34

35

36

37

38



39

40

41

42

43

44

45

46

47

48


49

50

51

52

53

54



55

56

57

58

59


60

61

62

63

64

65



66

67

68

69


70

71

72

73

74

75



76

77

78

79


80

81

82

83

84

85



86

87

88

89

90

91

92


93

94


95

96

97

98

99

100



101

102

103

104

105

106

107


108


109

110

111

112

113

114



115

116

117

118

119

120

121


122

123


124

125

126

127

128

129

130



131

132



133

134

135

136

137

138

139


140

141

142

143

144

145



146

147

148


149

150

151

152

153

154



155

156

157

158



159

160

161

162

163



164

165

166

167

168



169

170

171

172

173

174

175

176

177



178

179



180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199


200

201

202

203

204

205



206

207

208

209

210

211

212

213

214

215


216

217

218

219

220

221



222

223

224

225

226

227

228

229

230

231



232

233



234

235

236

237



238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253


254

255



256

257

258



259

260

261

262

263

264

265



266

267



268

269

270

271



272

273

274

275

276

277

278

279

280


281


282

283

284

285



286



287

288

289



290

291



292

293

294

295

296

297

298

299

300

一个完整的人生应该是宽恕、容忍、等待和爱!