[控件学习 - 2-2]Extra Valid TextBox[源码]
上次写了[控件学习-2]Extra Valid TextBox 学习了控件的如何扩展属性已及绑定脚本方法.
之后由于接了一个delphi的项目(其实我的主业是delphi),一直再加班赶工,没有再整理相关的一些代码出来.
觉得这个[控件学习-2]Extra Valid TextBox 还有很多可以改进的地方,先将最粗糙的代码帖上来,大家可以帮忙改进,或者提供更多的意见.
我也是在尝试修改各种不同需求来学习更多控件编写知识.
代码如下,需要的朋友可以看下,搬门弄斧了.......
KTextBox.cs
1using System;
2using System.Collections;
3using System.ComponentModel;
4using System.Drawing;
5using System.Web.UI;
6using System.Web.UI.WebControls;
7[assembly : TagPrefix("LangZi.WebControls", "KTX")]
8
9namespace LangZi.WebControls
10{
11 /**//// <summary>
12 /// 功能:
13 /// 能在客户端自动对多种类型数据进行验证的 TextBox
14 ///
15 /// 可验证的类型为:
16 /// 1.不为空
17 /// 2.和另一个输入框比较,看值是否相等
18 /// 3.日期时间
19 /// 4.日期
20 /// 5.时间
21 /// 6.Email地址
22 ///
23 /// 内置的几种常用正则表达式验证:
24 /// 1.
25 /// 2.
26 ///
27 /// 另外,如果设置为不进行任何验证,功能和普通的 TextBox 一样
28 ///
29 ///
30 /// 修改记录:
31 ///
32 /// 日期 修改人 主要修改说明
33 ///
34 /// 2005.9.8 陈达艺 新建
35 ///
36 ///
37 /// </summary>
38 [DefaultProperty("ValidType")]
39 [ToolboxData("<{0}:KTextBox runat=server></{0}:KTextBox>")]
40 public class KTextBox : System.Web.UI.WebControls.TextBox, INamingContainer
41 {
42 私有成员变量#region 私有成员变量
43
44 //枚举可以验证的类型
45 public enum AvailableType
46 {
47 NoSet, //不进行验证,功能和普通的 TextBox 一样
48 EmailAddress, //Email地址
49 RequiredField, //不为空
50 Compare, //和另一个控件比较,看值是否相等
51 RegularExpression, //正则表达式
52 DateTime, //日期时间
53 Date, //日期
54 Time, //时间
55 Num //数字
56 }
57
58 //验证时间
59 public enum ValidTime
60 {
61 OnBlur, //失去焦点时进行验证
62 OnSubmit, //页面提交时进行验证
63 All //失去焦点和页面提交时都进行验证
64 }
65
66 private Label lblMessage;
67 private AvailableType _validType;
68 private string _errorMessage;
69 private ValidTime _validMethod;
70 private string _compareControl;
71 private string _validRegularExpression;
72
73 #endregion 私有成员变量
74
75 公有属性#region 公有属性
76
77 /**//// <summary>
78 /// Set/Get trimmed text of textbox
79 /// </summary>
80 [Browsable(false)]
81 [Description("Set/Get trimmed text of textbox")]
82 public string TextTrimed
83 {
84 get { return this.Text.Trim(); }
85 set { this.Text = value.Trim(); }
86 }
87
88 /**//// <summary>
89 /// Get/Set value datetime of textbox
90 /// </summary>
91 [Browsable(false)]
92 [Description("Get/Set value datetime of textbox")]
93 public DateTime DateTimeValue
94 {
95 get
96 {
97 try
98 {
99 DateTime dtm = DateTime.Parse(this.TextTrimed);
100 return dtm;
101 }
102 catch (Exception exp)
103 {
104 throw exp;
105 }
106 }
107 set { this.Text = value.ToString(); }
108 }
109
110
111 /**//// <summary>
112 /// Check whether the textbox is empty, if it is, return true
113 /// </summary>
114 [Browsable(false)]
115 [Description("Check whether the textbox is empty, if it is, return true")]
116 public bool IsEmpty
117 {
118 get { return this.Text.Length == 0; }
119 }
120
121 /**//// <summary>
122 /// Get whether the value of textbox is the type int
123 /// </summary>
124 [Browsable(false)]
125 [Description("Get whether the value of textbox is the type int")]
126 public bool IsInt
127 {
128 get
129 {
130 try
131 {
132 int v = int.Parse(this.TextTrimed);
133 return true;
134 }
135 catch
136 {
137 return false;
138 }
139 }
140 }
141
142 /**//// <summary>
143 /// 获取和设置进行验证的数据类型
144 /// </summary>
145 [Category("验证")]
146 [Browsable(true)]
147 [Description("进行验证的数据类型")]
148 public AvailableType ValidType
149 {
150 get { return _validType; }
151 set { _validType = value; }
152 }
153
154 /**//// <summary>
155 /// 获取和设置验证错误时的提示信息
156 /// </summary>
157 [Category("验证")]
158 [Browsable(true)]
159 [Description("验证错误时的提示信息")]
160 public string ErrorMessage
161 {
162 get { return _errorMessage; }
163 set { _errorMessage = value; }
164 }
165
166 /**//// <summary>
167 /// 获取和设置触发验证的方法
168 /// </summary>
169 [Category("验证")]
170 [Browsable(true)]
171 [Description("触发验证的方法")]
172 public ValidTime ValidMethod
173 {
174 get { return _validMethod; }
175 set { _validMethod = value; }
176 }
177
178 /**//// <summary>
179 /// 获取和设置进行值比较的控件
180 /// </summary>
181 [Category("验证")]
182 [Browsable(true)]
183 [TypeConverter(typeof (CompareControlConverter))]
184 [Description("与此控件进行值比较的控件")]
185 public string CompareControl
186 {
187 get { return _compareControl; }
188 set { _compareControl = value; }
189 }
190
191 /**//// <summary>
192 /// 获取和设置进行验证的正则表达式
193 /// </summary>
194 [Category("验证")]
195 [Browsable(true)]
196 [Description("进行验证的正则表达式")]
197 public string ValidRegularExpression
198 {
199 get { return _validRegularExpression; }
200 set { _validRegularExpression = value; }
201 }
202
203 #endregion 公有属性
204
205 创建控件层次结构#region 创建控件层次结构
206
207 /**//// <summary>
208 /// 创建子控件
209 /// </summary>
210 protected override void CreateChildControls()
211 {
212 base.CreateChildControls();
213
214 lblMessage = new Label();
215 lblMessage.ID = "lblMessage";
216 lblMessage.ControlStyle.ForeColor = Color.Red;
217 lblMessage.EnableViewState = true;
218
219 AddAttributesInf();
220
221
222 this.Controls.Add(lblMessage);
223 }
224
225 /**//// <summary>
226 /// 向 lblMessage 添加一些名值对信息
227 /// </summary>
228 private void AddAttributesInf()
229 {
230 //根据验证的类型这些名值对信息不相同
231 switch (ValidType)
232 {
233 case AvailableType.NoSet:
234 break;
235 case AvailableType.EmailAddress:
236 lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
237 lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
238 break;
239 case AvailableType.RequiredField:
240 lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
241 lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
242 break;
243 case AvailableType.Compare:
244 lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
245 lblMessage.Attributes.Add("controltocompare", CompareControl);
246 lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
247 break;
248 case AvailableType.RegularExpression:
249 lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
250 lblMessage.Attributes.Add("validRegularexpression", ValidRegularExpression);
251 lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
252 break;
253 case AvailableType.DateTime:
254 lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
255 lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
256 break;
257 case AvailableType.Date:
258 lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
259 lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
260 break;
261 case AvailableType.Time:
262 lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
263 lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
264 break;
265 case AvailableType.Num:
266 lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
267 lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
268 break;
269
270 }
271
272 }
273
274 /**//// <summary>
275 /// 由于继承于 TextBox 控件,RenderContents 方法不会自动被系统调用,
276 /// 所以在 RenderEndTag 方法中调用 RenderContents ,用来呈现 CreateChildControls 方法中添加的控件,
277 /// 如果不这样,CreateChildControls 方法中添加的控件不会呈现在客户端
278 /// </summary>
279 /// <param name="writer"></param>
280 public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
281 {
282 base.RenderEndTag(writer);
283 this.RenderContents(writer);
284 }
285
286 #endregion 创建控件层次结构
287
288 注册 javascript#region 注册 javascript
289
290 /**//// <summary>
291 /// 预呈现,在这里向客户端注册 javascript 块
292 /// </summary>
293 /// <param name="e"></param>
294 protected override void OnPreRender(EventArgs e)
295 {
296 base.OnPreRender(e);
297
298 //设置为进行验证时才向客户端注册 javascript
299 if (ValidType != AvailableType.NoSet)
300 {
301 RegisterClientScript(ValidType);
302
303 //注册客户端对象数组
304 Page.RegisterArrayDeclaration("Valid_Spans", "document.getElementById(\"" + this.UniqueID + "_lblMessage\")");
305 //注册页面提交时的 javascript
306 Page.RegisterOnSubmitStatement("ValidatorOnSubmit", "return ValidatorOnSubmit();");
307 }
308 }
309
310 /**//// <summary>
311 /// 注册客户端 javascript
312 /// </summary>
313 /// <param name="vaildType">验证类型</param>
314 private void RegisterClientScript(AvailableType vaildType)
315 {
316 string registerKey = vaildType.ToString();
317
318 ClientScript clientScript = new ClientScript();
319
320
321 //注册表单提交时的验证 javascript
322 if ((ValidMethod == ValidTime.All) || (ValidMethod == ValidTime.OnSubmit))
323 {
324 if (!Page.IsClientScriptBlockRegistered("validatorOnSubmit"))
325 {
326 Page.RegisterClientScriptBlock("validatorOnSubmit", clientScript.GetValidatorOnSubmit());
327 }
328 }
329
330
331 //注册对指定类型数据进行验证的 javascript
332 if (!Page.IsClientScriptBlockRegistered(registerKey))
333 {
334 Page.RegisterClientScriptBlock(registerKey, GetClientScript(vaildType));
335 }
336
337 //如果设置为在失去焦点的时候进行验证,注册 onBlur 事件
338 if ((ValidMethod == ValidTime.All) || (ValidMethod == ValidTime.OnBlur))
339 {
340 //处理事件的函数
341 string validFunction = "Is" + ValidType.ToString() + "('" + this.UniqueID + "', '" + this.UniqueID + "_lblMessage')";
342
343 base.Attributes.Add("onBlur", validFunction);
344 }
345 }
346
347 /**//// <summary>
348 /// 根据需要验证的数据类型获取验证的 javascript
349 /// </summary>
350 /// <param name="vaildType">需要验证的数据类型</param>
351 /// <returns>javascript</returns>
352 private string GetClientScript(AvailableType vaildType)
353 {
354 string script = "";
355
356 //实例化创建脚本的对象
357 ClientScript clientScript = new ClientScript();
358
359 //根据验证类型获取验证 javascript
360 switch (vaildType)
361 {
362 case AvailableType.EmailAddress:
363 script = clientScript.GetIsEmailAddressScript(ErrorMessage);
364 break;
365 case AvailableType.RequiredField:
366 script = clientScript.GetIsRequiredFieldScript(ErrorMessage);
367 break;
368 case AvailableType.Compare:
369 script = clientScript.GetIsCompareScript(ErrorMessage);
370 break;
371 case AvailableType.RegularExpression:
372 script = clientScript.GetIsRegularExpressionScript(ErrorMessage);
373 break;
374 case AvailableType.DateTime:
375 script = clientScript.GetIsDatetimeScript(ErrorMessage);
376 break;
377 case AvailableType.Date:
378 script = clientScript.GetIsDateScript(ErrorMessage);
379 break;
380 case AvailableType.Time:
381 script = clientScript.GetIsTimeScript(ErrorMessage);
382 break;
383 case AvailableType.Num:
384 script = clientScript.GetIsNumScript(ErrorMessage);
385 break;
386
387 }
388
389 return script;
390
391 }
392
393 #endregion 注册 javascript
394 }
395
396 class CompareControlConverter : TypeConverter#region class CompareControlConverter : TypeConverter
397
398 /**//// <summary>
399 /// 对 KTextBox 控件 CompareControl 属性值进行类型转换的类
400 /// </summary>
401 public class CompareControlConverter : TypeConverter
402 {
403 public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
404 {
405 return true;
406 }
407
408 public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
409 {
410 return true;
411 }
412
413 public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
414 {
415 ArrayList controlList = new ArrayList();
416
417 foreach (IComponent comp in context.Container.Components)
418 {
419 if (comp is KTextBox)
420 {
421 controlList.Add(((KTextBox) comp).ID);
422 }
423 }
424
425 return new StandardValuesCollection(controlList);
426 }
427
428 }
429
430 #endregion class CompareControlConverter : TypeConverter
431}
2using System.Collections;
3using System.ComponentModel;
4using System.Drawing;
5using System.Web.UI;
6using System.Web.UI.WebControls;
7[assembly : TagPrefix("LangZi.WebControls", "KTX")]
8
9namespace LangZi.WebControls
10{
11 /**//// <summary>
12 /// 功能:
13 /// 能在客户端自动对多种类型数据进行验证的 TextBox
14 ///
15 /// 可验证的类型为:
16 /// 1.不为空
17 /// 2.和另一个输入框比较,看值是否相等
18 /// 3.日期时间
19 /// 4.日期
20 /// 5.时间
21 /// 6.Email地址
22 ///
23 /// 内置的几种常用正则表达式验证:
24 /// 1.
25 /// 2.
26 ///
27 /// 另外,如果设置为不进行任何验证,功能和普通的 TextBox 一样
28 ///
29 ///
30 /// 修改记录:
31 ///
32 /// 日期 修改人 主要修改说明
33 ///
34 /// 2005.9.8 陈达艺 新建
35 ///
36 ///
37 /// </summary>
38 [DefaultProperty("ValidType")]
39 [ToolboxData("<{0}:KTextBox runat=server></{0}:KTextBox>")]
40 public class KTextBox : System.Web.UI.WebControls.TextBox, INamingContainer
41 {
42 私有成员变量#region 私有成员变量
43
44 //枚举可以验证的类型
45 public enum AvailableType
46 {
47 NoSet, //不进行验证,功能和普通的 TextBox 一样
48 EmailAddress, //Email地址
49 RequiredField, //不为空
50 Compare, //和另一个控件比较,看值是否相等
51 RegularExpression, //正则表达式
52 DateTime, //日期时间
53 Date, //日期
54 Time, //时间
55 Num //数字
56 }
57
58 //验证时间
59 public enum ValidTime
60 {
61 OnBlur, //失去焦点时进行验证
62 OnSubmit, //页面提交时进行验证
63 All //失去焦点和页面提交时都进行验证
64 }
65
66 private Label lblMessage;
67 private AvailableType _validType;
68 private string _errorMessage;
69 private ValidTime _validMethod;
70 private string _compareControl;
71 private string _validRegularExpression;
72
73 #endregion 私有成员变量
74
75 公有属性#region 公有属性
76
77 /**//// <summary>
78 /// Set/Get trimmed text of textbox
79 /// </summary>
80 [Browsable(false)]
81 [Description("Set/Get trimmed text of textbox")]
82 public string TextTrimed
83 {
84 get { return this.Text.Trim(); }
85 set { this.Text = value.Trim(); }
86 }
87
88 /**//// <summary>
89 /// Get/Set value datetime of textbox
90 /// </summary>
91 [Browsable(false)]
92 [Description("Get/Set value datetime of textbox")]
93 public DateTime DateTimeValue
94 {
95 get
96 {
97 try
98 {
99 DateTime dtm = DateTime.Parse(this.TextTrimed);
100 return dtm;
101 }
102 catch (Exception exp)
103 {
104 throw exp;
105 }
106 }
107 set { this.Text = value.ToString(); }
108 }
109
110
111 /**//// <summary>
112 /// Check whether the textbox is empty, if it is, return true
113 /// </summary>
114 [Browsable(false)]
115 [Description("Check whether the textbox is empty, if it is, return true")]
116 public bool IsEmpty
117 {
118 get { return this.Text.Length == 0; }
119 }
120
121 /**//// <summary>
122 /// Get whether the value of textbox is the type int
123 /// </summary>
124 [Browsable(false)]
125 [Description("Get whether the value of textbox is the type int")]
126 public bool IsInt
127 {
128 get
129 {
130 try
131 {
132 int v = int.Parse(this.TextTrimed);
133 return true;
134 }
135 catch
136 {
137 return false;
138 }
139 }
140 }
141
142 /**//// <summary>
143 /// 获取和设置进行验证的数据类型
144 /// </summary>
145 [Category("验证")]
146 [Browsable(true)]
147 [Description("进行验证的数据类型")]
148 public AvailableType ValidType
149 {
150 get { return _validType; }
151 set { _validType = value; }
152 }
153
154 /**//// <summary>
155 /// 获取和设置验证错误时的提示信息
156 /// </summary>
157 [Category("验证")]
158 [Browsable(true)]
159 [Description("验证错误时的提示信息")]
160 public string ErrorMessage
161 {
162 get { return _errorMessage; }
163 set { _errorMessage = value; }
164 }
165
166 /**//// <summary>
167 /// 获取和设置触发验证的方法
168 /// </summary>
169 [Category("验证")]
170 [Browsable(true)]
171 [Description("触发验证的方法")]
172 public ValidTime ValidMethod
173 {
174 get { return _validMethod; }
175 set { _validMethod = value; }
176 }
177
178 /**//// <summary>
179 /// 获取和设置进行值比较的控件
180 /// </summary>
181 [Category("验证")]
182 [Browsable(true)]
183 [TypeConverter(typeof (CompareControlConverter))]
184 [Description("与此控件进行值比较的控件")]
185 public string CompareControl
186 {
187 get { return _compareControl; }
188 set { _compareControl = value; }
189 }
190
191 /**//// <summary>
192 /// 获取和设置进行验证的正则表达式
193 /// </summary>
194 [Category("验证")]
195 [Browsable(true)]
196 [Description("进行验证的正则表达式")]
197 public string ValidRegularExpression
198 {
199 get { return _validRegularExpression; }
200 set { _validRegularExpression = value; }
201 }
202
203 #endregion 公有属性
204
205 创建控件层次结构#region 创建控件层次结构
206
207 /**//// <summary>
208 /// 创建子控件
209 /// </summary>
210 protected override void CreateChildControls()
211 {
212 base.CreateChildControls();
213
214 lblMessage = new Label();
215 lblMessage.ID = "lblMessage";
216 lblMessage.ControlStyle.ForeColor = Color.Red;
217 lblMessage.EnableViewState = true;
218
219 AddAttributesInf();
220
221
222 this.Controls.Add(lblMessage);
223 }
224
225 /**//// <summary>
226 /// 向 lblMessage 添加一些名值对信息
227 /// </summary>
228 private void AddAttributesInf()
229 {
230 //根据验证的类型这些名值对信息不相同
231 switch (ValidType)
232 {
233 case AvailableType.NoSet:
234 break;
235 case AvailableType.EmailAddress:
236 lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
237 lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
238 break;
239 case AvailableType.RequiredField:
240 lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
241 lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
242 break;
243 case AvailableType.Compare:
244 lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
245 lblMessage.Attributes.Add("controltocompare", CompareControl);
246 lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
247 break;
248 case AvailableType.RegularExpression:
249 lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
250 lblMessage.Attributes.Add("validRegularexpression", ValidRegularExpression);
251 lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
252 break;
253 case AvailableType.DateTime:
254 lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
255 lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
256 break;
257 case AvailableType.Date:
258 lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
259 lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
260 break;
261 case AvailableType.Time:
262 lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
263 lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
264 break;
265 case AvailableType.Num:
266 lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
267 lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
268 break;
269
270 }
271
272 }
273
274 /**//// <summary>
275 /// 由于继承于 TextBox 控件,RenderContents 方法不会自动被系统调用,
276 /// 所以在 RenderEndTag 方法中调用 RenderContents ,用来呈现 CreateChildControls 方法中添加的控件,
277 /// 如果不这样,CreateChildControls 方法中添加的控件不会呈现在客户端
278 /// </summary>
279 /// <param name="writer"></param>
280 public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
281 {
282 base.RenderEndTag(writer);
283 this.RenderContents(writer);
284 }
285
286 #endregion 创建控件层次结构
287
288 注册 javascript#region 注册 javascript
289
290 /**//// <summary>
291 /// 预呈现,在这里向客户端注册 javascript 块
292 /// </summary>
293 /// <param name="e"></param>
294 protected override void OnPreRender(EventArgs e)
295 {
296 base.OnPreRender(e);
297
298 //设置为进行验证时才向客户端注册 javascript
299 if (ValidType != AvailableType.NoSet)
300 {
301 RegisterClientScript(ValidType);
302
303 //注册客户端对象数组
304 Page.RegisterArrayDeclaration("Valid_Spans", "document.getElementById(\"" + this.UniqueID + "_lblMessage\")");
305 //注册页面提交时的 javascript
306 Page.RegisterOnSubmitStatement("ValidatorOnSubmit", "return ValidatorOnSubmit();");
307 }
308 }
309
310 /**//// <summary>
311 /// 注册客户端 javascript
312 /// </summary>
313 /// <param name="vaildType">验证类型</param>
314 private void RegisterClientScript(AvailableType vaildType)
315 {
316 string registerKey = vaildType.ToString();
317
318 ClientScript clientScript = new ClientScript();
319
320
321 //注册表单提交时的验证 javascript
322 if ((ValidMethod == ValidTime.All) || (ValidMethod == ValidTime.OnSubmit))
323 {
324 if (!Page.IsClientScriptBlockRegistered("validatorOnSubmit"))
325 {
326 Page.RegisterClientScriptBlock("validatorOnSubmit", clientScript.GetValidatorOnSubmit());
327 }
328 }
329
330
331 //注册对指定类型数据进行验证的 javascript
332 if (!Page.IsClientScriptBlockRegistered(registerKey))
333 {
334 Page.RegisterClientScriptBlock(registerKey, GetClientScript(vaildType));
335 }
336
337 //如果设置为在失去焦点的时候进行验证,注册 onBlur 事件
338 if ((ValidMethod == ValidTime.All) || (ValidMethod == ValidTime.OnBlur))
339 {
340 //处理事件的函数
341 string validFunction = "Is" + ValidType.ToString() + "('" + this.UniqueID + "', '" + this.UniqueID + "_lblMessage')";
342
343 base.Attributes.Add("onBlur", validFunction);
344 }
345 }
346
347 /**//// <summary>
348 /// 根据需要验证的数据类型获取验证的 javascript
349 /// </summary>
350 /// <param name="vaildType">需要验证的数据类型</param>
351 /// <returns>javascript</returns>
352 private string GetClientScript(AvailableType vaildType)
353 {
354 string script = "";
355
356 //实例化创建脚本的对象
357 ClientScript clientScript = new ClientScript();
358
359 //根据验证类型获取验证 javascript
360 switch (vaildType)
361 {
362 case AvailableType.EmailAddress:
363 script = clientScript.GetIsEmailAddressScript(ErrorMessage);
364 break;
365 case AvailableType.RequiredField:
366 script = clientScript.GetIsRequiredFieldScript(ErrorMessage);
367 break;
368 case AvailableType.Compare:
369 script = clientScript.GetIsCompareScript(ErrorMessage);
370 break;
371 case AvailableType.RegularExpression:
372 script = clientScript.GetIsRegularExpressionScript(ErrorMessage);
373 break;
374 case AvailableType.DateTime:
375 script = clientScript.GetIsDatetimeScript(ErrorMessage);
376 break;
377 case AvailableType.Date:
378 script = clientScript.GetIsDateScript(ErrorMessage);
379 break;
380 case AvailableType.Time:
381 script = clientScript.GetIsTimeScript(ErrorMessage);
382 break;
383 case AvailableType.Num:
384 script = clientScript.GetIsNumScript(ErrorMessage);
385 break;
386
387 }
388
389 return script;
390
391 }
392
393 #endregion 注册 javascript
394 }
395
396 class CompareControlConverter : TypeConverter#region class CompareControlConverter : TypeConverter
397
398 /**//// <summary>
399 /// 对 KTextBox 控件 CompareControl 属性值进行类型转换的类
400 /// </summary>
401 public class CompareControlConverter : TypeConverter
402 {
403 public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
404 {
405 return true;
406 }
407
408 public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
409 {
410 return true;
411 }
412
413 public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
414 {
415 ArrayList controlList = new ArrayList();
416
417 foreach (IComponent comp in context.Container.Components)
418 {
419 if (comp is KTextBox)
420 {
421 controlList.Add(((KTextBox) comp).ID);
422 }
423 }
424
425 return new StandardValuesCollection(controlList);
426 }
427
428 }
429
430 #endregion class CompareControlConverter : TypeConverter
431}
脚本生成:
1using System.Text;
2
3namespace LangZi.WebControls
4{
5 /**//// <summary>
6 /// 功能:
7 /// 生成客户端脚本
8 ///
9 ///
10 /// 修改记录:
11 ///
12 /// 日期 修改人 主要修改说明
13 ///
14 /// 2005.9.8 陈达艺 新建
15 ///
16 ///
17 /// </summary>
18 public class ClientScript
19 {
20 Constructor#region Constructor
21
22 public ClientScript()
23 {
24 }
25
26 #endregion
27
28 KTextBox#region KTextBox
29
30 /**//// <summary>
31 /// 获取表单提交时的验证 javascript
32 /// </summary>
33 /// <returns>表单提交时的验证 javascript</returns>
34 public string GetValidatorOnSubmit()
35 {
36 StringBuilder validatorOnSubmit = new StringBuilder();
37
38 validatorOnSubmit.Append("<script language=javascript> ");
39 validatorOnSubmit.Append("function ValidatorOnSubmit() ");
40 validatorOnSubmit.Append("{ ");
41 validatorOnSubmit.Append(" var valid = true; ");
42 validatorOnSubmit.Append(" var i; ");
43 validatorOnSubmit.Append(" for (i = 0; i < Valid_Spans.length; i++) ");
44 validatorOnSubmit.Append(" { ");
45 validatorOnSubmit.Append(" var validFunction = Valid_Spans[i].evaluationfunction; ");
46 validatorOnSubmit.Append(" var inputID = Valid_Spans[i].controltovalidate; ");
47 validatorOnSubmit.Append(" var spanID = Valid_Spans[i].id; ");
48 validatorOnSubmit.Append(" if (!eval(validFunction + \"(inputID,spanID)\")) ");
49 validatorOnSubmit.Append(" { ");
50 validatorOnSubmit.Append(" valid = false; ");
51 validatorOnSubmit.Append(" } ");
52 validatorOnSubmit.Append(" } ");
53 validatorOnSubmit.Append(" return valid; ");
54 validatorOnSubmit.Append("} ");
55 validatorOnSubmit.Append("</script> ");
56
57 return validatorOnSubmit.ToString();
58
59 }
60
61 /**//// <summary>
62 /// 获取验证 Email 地址的 javascript
63 /// </summary>
64 /// <param name="errMessage">验证错误时的提示信息</param>
65 /// <returns>验证 javascript</returns>
66 public string GetIsEmailAddressScript(string errMessage)
67 {
68 StringBuilder emailAddressScript = new StringBuilder();
69
70 emailAddressScript.Append("<script language=javascript> ");
71 emailAddressScript.Append("function IsEmailAddress(inputID, spanID) ");
72 emailAddressScript.Append("{ ");
73 emailAddressScript.Append(" var emialAddress = document.getElementById(inputID).value; ");
74 emailAddressScript.Append(" " + @"if(emialAddress.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)!= -1)" + " ");
75 emailAddressScript.Append(" { ");
76 emailAddressScript.Append(" document.getElementById(spanID).innerText = ''; ");
77 emailAddressScript.Append(" return true; ");
78 emailAddressScript.Append(" } ");
79 emailAddressScript.Append(" else ");
80 emailAddressScript.Append(" { ");
81 emailAddressScript.Append(" alert('" + errMessage + "'); ");
82 emailAddressScript.Append(" document.getElementById(inputID).focus(); ");
83 emailAddressScript.Append(" return false; ");
84 emailAddressScript.Append(" } ");
85 emailAddressScript.Append("} ");
86 emailAddressScript.Append("</script> ");
87
88 return emailAddressScript.ToString();
89 }
90
91 /**//// <summary>
92 /// 获取验证不为空的 javascript
93 /// </summary>
94 /// <param name="errMessage">验证错误时的提示信息</param>
95 /// <returns>验证 javascript</returns>
96 public string GetIsRequiredFieldScript(string errMessage)
97 {
98 StringBuilder requiredFieldScript = new StringBuilder();
99
100 requiredFieldScript.Append("<script language=javascript> ");
101 requiredFieldScript.Append("function IsRequiredField(inputID, spanID) ");
102 requiredFieldScript.Append("{ ");
103 requiredFieldScript.Append(" var inputValue = document.getElementById(inputID).value; ");
104 requiredFieldScript.Append(" if (inputValue != \"\") ");
105 requiredFieldScript.Append(" { ");
106 requiredFieldScript.Append(" document.getElementById(spanID).innerText = ''; ");
107 requiredFieldScript.Append(" return true; ");
108 requiredFieldScript.Append(" } ");
109 requiredFieldScript.Append(" else ");
110 requiredFieldScript.Append(" { ");
111 requiredFieldScript.Append(" alert('" + errMessage + "'); ");
112 requiredFieldScript.Append(" document.getElementById(inputID).focus(); ");
113 requiredFieldScript.Append(" return false; ");
114 requiredFieldScript.Append(" } ");
115 requiredFieldScript.Append(" ");
116 requiredFieldScript.Append(" ");
117 requiredFieldScript.Append(" ");
118 requiredFieldScript.Append("} ");
119 requiredFieldScript.Append("</script> ");
120
121 return requiredFieldScript.ToString();
122 }
123
124 /**//// <summary>
125 /// 获取验证二个 input 值是否相等的 javascript
126 /// </summary>
127 /// <param name="errMessage">验证错误时的提示信息</param>
128 /// <returns>验证二个 input 值是否相等的 javascript</returns>
129 public string GetIsCompareScript(string errMessage)
130 {
131 StringBuilder compareScript = new StringBuilder();
132
133 compareScript.Append("<script language=javascript> ");
134 compareScript.Append("function IsCompare(inputID, spanID) ");
135 compareScript.Append("{ ");
136 compareScript.Append(" var inputValue = document.getElementById(inputID).value; ");
137 compareScript.Append(" var compareID = document.getElementById(spanID).controltocompare; ");
138
139 compareScript.Append(" if (compareID == \"\") ");
140 compareScript.Append(" { ");
141 compareScript.Append(" alert('没有设置与ID为' + inputID + '的输入框比较的对象!'); ");
142 compareScript.Append(" return; ");
143 compareScript.Append(" } ");
144
145 compareScript.Append(" if (document.getElementById(compareID) == null) ");
146 compareScript.Append(" { ");
147 compareScript.Append(" alert('与ID为' + inputID + '的输入框比较的对象不存在!'); ");
148 compareScript.Append(" return; ");
149 compareScript.Append(" } ");
150
151 compareScript.Append(" var compareValue = document.getElementById(compareID).value; ");
152
153 compareScript.Append(" if (inputValue == compareValue) ");
154 compareScript.Append(" { ");
155 compareScript.Append(" document.getElementById(spanID).innerText = ''; ");
156 compareScript.Append(" return true; ");
157 compareScript.Append(" } ");
158 compareScript.Append(" else ");
159 compareScript.Append(" { ");
160 compareScript.Append(" alert('" + errMessage + "'); ");
161 compareScript.Append(" document.getElementById(inputID).focus(); ");
162 compareScript.Append(" return false; ");
163 compareScript.Append(" } ");
164
165 compareScript.Append("} ");
166 compareScript.Append("</script> ");
167
168
169 return compareScript.ToString();
170 }
171
172 /**//// <summary>
173 /// 获取输入数据是否匹配指定的正则表达式的 javascript
174 /// </summary>
175 /// <param name="errMessage">验证错误时的提示信息</param>
176 /// <returns>输入数据是否匹配指定的正则表达式的 javascript</returns>
177 public string GetIsRegularExpressionScript(string errMessage)
178 {
179 StringBuilder regularExpressionScript = new StringBuilder();
180
181 regularExpressionScript.Append("<script language=javascript> ");
182 regularExpressionScript.Append("function IsRegularExpression(inputID, spanID) ");
183 regularExpressionScript.Append("{ ");
184 regularExpressionScript.Append(" var inputValue = document.getElementById(inputID).value; ");
185 regularExpressionScript.Append(" var regularExpression = document.getElementById(spanID).validRegularexpression ");
186 regularExpressionScript.Append(" if (typeof(eval(regularExpression)) == \"undefined\") ");
187 regularExpressionScript.Append(" { ");
188 regularExpressionScript.Append(" alert('正则表达式不正确!'); ");
189 regularExpressionScript.Append(" return; ");
190 regularExpressionScript.Append(" } ");
191
192 regularExpressionScript.Append(" if (eval(regularExpression).test(inputValue)) ");
193 regularExpressionScript.Append(" { ");
194 regularExpressionScript.Append(" document.getElementById(spanID).innerText = ''; ");
195 regularExpressionScript.Append(" return true; ");
196 regularExpressionScript.Append(" } ");
197 regularExpressionScript.Append(" else ");
198 regularExpressionScript.Append(" { ");
199 regularExpressionScript.Append(" alert('" + errMessage + "'); ");
200 regularExpressionScript.Append(" document.getElementById(inputID).focus(); ");
201 regularExpressionScript.Append(" return false; ");
202 regularExpressionScript.Append(" } ");
203
204 regularExpressionScript.Append("} ");
205 regularExpressionScript.Append("</script> ");
206
207 return regularExpressionScript.ToString();
208
209 }
210
211 /**//// <summary>
212 /// 获取验证日期和时间的 javascript
213 /// </summary>
214 /// <param name="errMessage">验证错误时的提示信息</param>
215 /// <returns>验证日期和时间的 javascript</returns>
216 public string GetIsDatetimeScript(string errMessage)
217 {
218 StringBuilder datetimeScript = new StringBuilder();
219
220 datetimeScript.Append("<script language=javascript> ");
221 datetimeScript.Append("function IsDateTime(inputID, spanID) ");
222 datetimeScript.Append("{ ");
223
224 datetimeScript.Append(" var inputValue = document.getElementById(inputID).value; ");
225
226 datetimeScript.Append(" if(inputValue == '') ");
227 datetimeScript.Append(" { ");
228// datetimeScript.Append(" document.getElementById(spanID).innerText = '请输入日期和时间'; ");
229 datetimeScript.Append(" alert('请输入日期和时间!')");
230 datetimeScript.Append(" return false; ");
231 datetimeScript.Append(" } ");
232
233 datetimeScript.Append(" var reg = /^(\\d{1,4})(-|\\/|\\.)(\\d{1,2})\\2(\\d{1,2}) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})$/; ");
234 datetimeScript.Append(" var r = inputValue.match(reg); ");
235
236 datetimeScript.Append(" if(r == null) ");
237 datetimeScript.Append(" { ");
238 datetimeScript.Append(" alert('" + errMessage + "'); ");
239 datetimeScript.Append(" document.getElementById(inputID).focus(); ");
240 datetimeScript.Append(" return false; ");
241 datetimeScript.Append(" } ");
242
243 datetimeScript.Append(" var d= new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]); ");
244
245 datetimeScript.Append(" if(r[1] != d.getFullYear() || r[3] != d.getMonth() + 1 || r[4] != d.getDate() || r[5] != d.getHours() || r[6] != d.getMinutes() || r[7] != d.getSeconds()) ");
246 datetimeScript.Append(" { ");
247 datetimeScript.Append(" alert('" + errMessage + "'); ");
248 datetimeScript.Append(" document.getElementById(inputID).focus(); ");
249 datetimeScript.Append(" return false; ");
250 datetimeScript.Append(" } ");
251
252 datetimeScript.Append(" if(r[3].length == 1) r[3] = '0' + r[3]; ");
253 datetimeScript.Append(" if(r[4].length == 1) r[4] = '0' + r[4]; ");
254 datetimeScript.Append(" if(r[5].length == 1) r[5] = '0' + r[5]; ");
255 datetimeScript.Append(" if(r[6].length == 1) r[6] = '0' + r[6]; ");
256 datetimeScript.Append(" if(r[7].length == 1) r[7] = '0' + r[7]; ");
257 datetimeScript.Append(" document.getElementById(inputID).value = r[1] + '-' + r[3] + '-' + r[4] + ' ' + r[5] + ':' + r[6] + ':' + r[7]; ");
258 datetimeScript.Append(" document.getElementById(spanID).innerText = ''; ");
259
260 datetimeScript.Append(" return true; ");
261 datetimeScript.Append("} ");
262 datetimeScript.Append("</script> ");
263
264 return datetimeScript.ToString();
265
266 }
267
268 /**//// <summary>
269 /// 获取验证日期的 javascript
270 /// </summary>
271 /// <param name="errMessage">验证错误时的提示信息</param>
272 /// <returns>验证日期的 javascript</returns>
273 public string GetIsDateScript(string errMessage)
274 {
275 StringBuilder dateScript = new StringBuilder();
276
277 dateScript.Append("<script language=javascript> ");
278 dateScript.Append("function IsDate(inputID, spanID) ");
279 dateScript.Append("{ ");
280
281 dateScript.Append(" var inputValue = document.getElementById(inputID).value; ");
282
283 dateScript.Append(" if(inputValue == '') ");
284 dateScript.Append(" { ");
285// dateScript.Append(" document.getElementById(spanID).innerText = '请输入日期'; ");
286 dateScript.Append(" alert('请输入日期!');");
287 dateScript.Append(" return false; ");
288 dateScript.Append(" } ");
289
290 dateScript.Append(" var reg = /^(\\d{1,4})(-|\\/|\\.)(\\d{1,2})\\2(\\d{1,2})$/; ");
291 dateScript.Append(" var r = inputValue.match(reg); ");
292
293 dateScript.Append(" if(r == null) ");
294 dateScript.Append(" { ");
295 dateScript.Append(" alert('" + errMessage + "'); ");
296 dateScript.Append(" document.getElementById(inputID).focus(); ");
297 dateScript.Append(" return false; ");
298 dateScript.Append(" } ");
299
300 dateScript.Append(" var d = new Date(r[1], r[3]-1, r[4]) ");
301
302 dateScript.Append(" if(r[1] != d.getFullYear() || r[3] != d.getMonth() + 1 || r[4] != d.getDate()) ");
303 dateScript.Append(" { ");
304 dateScript.Append(" alert('" + errMessage + "'); ");
305 dateScript.Append(" document.getElementById(inputID).focus(); ");
306 dateScript.Append(" return false; ");
307 dateScript.Append(" } ");
308
309
310 dateScript.Append(" if(r[3].length == 1) r[3] = '0' + r[3]; ");
311 dateScript.Append(" if(r[4].length == 1) r[4 ]= '0' + r[4]; ");
312
313
314 dateScript.Append(" var newValue = r[1] + '-' + r[3] + '-' + r[4]; ");
315 dateScript.Append(" document.getElementById(inputID).value = newValue; ");
316 dateScript.Append(" document.getElementById(spanID).innerText = ''; ");
317
318 dateScript.Append(" return true; ");
319 dateScript.Append("} ");
320 dateScript.Append("</script>");
321
322 return dateScript.ToString();
323 }
324
325
326 /**//// <summary>
327 /// 获取验证时间的 javascript
328 /// </summary>
329 /// <param name="errMessage">验证错误时的提示信息</param>
330 /// <returns>验证时间的 javascript</returns>
331 public string GetIsTimeScript(string errMessage)
332 {
333 StringBuilder timeScript = new StringBuilder();
334
335 timeScript.Append("<script language=javascript> ");
336 timeScript.Append("function IsTime(inputID, spanID) ");
337 timeScript.Append("{ ");
338 timeScript.Append(" var inputValue = document.getElementById(inputID).value; ");
339
340 timeScript.Append(" if(inputValue == '') ");
341 timeScript.Append(" { ");
342// timeScript.Append(" document.getElementById(spanID).innerText = '请输入时间'; ");
343 timeScript.Append(" alert('请输入时间');");
344 timeScript.Append(" return false; ");
345 timeScript.Append(" } ");
346
347
348 timeScript.Append(" var reg = /^(\\d{1,2}):(\\d{1,2}):(\\d{1,2})$/; ");
349 timeScript.Append(" var r = inputValue.match(reg); ");
350
351 timeScript.Append(" if(r == null) ");
352 timeScript.Append(" { ");
353 timeScript.Append(" alert('" + errMessage + "'); ");
354 timeScript.Append(" document.getElementById(inputID).focus(); ");
355
356 timeScript.Append(" return false; ");
357 timeScript.Append(" } ");
358
359 timeScript.Append(" if(r[1] > 24 || r[2] > 60 || r[3] > 60) ");
360 timeScript.Append(" { ");
361 timeScript.Append(" alert('" + errMessage + "'); ");
362 timeScript.Append(" document.getElementById(inputID).focus(); ");
363 timeScript.Append(" return false; ");
364 timeScript.Append(" } ");
365
366 timeScript.Append(" if(r[1].length == 1) r[1] = '0' + r[1]; ");
367 timeScript.Append(" if(r[2].length == 1) r[2] = '0' + r[2]; ");
368 timeScript.Append(" if(r[3].length == 1) r[3] = '0' + r[3]; ");
369
370 timeScript.Append(" document.getElementById(inputID).value = r[1] + ':' + r[2] + ':' + r[3]; ");
371 timeScript.Append(" document.getElementById(spanID).innerText = ''; ");
372
373 timeScript.Append(" return true; ");
374 timeScript.Append("} ");
375 timeScript.Append("</script> ");
376
377 return timeScript.ToString();
378 }
379
380
381 /**//// <summary>
382 /// 获取验证时间的 javascript
383 /// </summary>
384 /// <param name="errMessage">验证错误时的提示信息</param>
385 /// <returns>验证时间的 javascript</returns>
386 public string GetIsNumScript(string errMessage)
387 {
388 StringBuilder numScript = new StringBuilder();
389
390 numScript.Append("<script language=javascript> ");
391 numScript.Append("function IsNum(inputID, spanID) ");
392 numScript.Append("{ ");
393 numScript.Append(" var inputValue = document.getElementById(inputID).value; ");
394 numScript.Append(" if(isNaN(inputValue)) ");
395 numScript.Append(" { ");
396 numScript.Append(" alert('请输入数字!'); ");
397 numScript.Append(" document.getElementById(inputID).focus(); ");
398 numScript.Append(" return false; ");
399 numScript.Append(" } ");
400
401
402 numScript.Append(" return true; ");
403 numScript.Append("} ");
404 numScript.Append("</script> ");
405
406 return numScript.ToString();
407 }
408
409 #endregion
410 }
411}
2
3namespace LangZi.WebControls
4{
5 /**//// <summary>
6 /// 功能:
7 /// 生成客户端脚本
8 ///
9 ///
10 /// 修改记录:
11 ///
12 /// 日期 修改人 主要修改说明
13 ///
14 /// 2005.9.8 陈达艺 新建
15 ///
16 ///
17 /// </summary>
18 public class ClientScript
19 {
20 Constructor#region Constructor
21
22 public ClientScript()
23 {
24 }
25
26 #endregion
27
28 KTextBox#region KTextBox
29
30 /**//// <summary>
31 /// 获取表单提交时的验证 javascript
32 /// </summary>
33 /// <returns>表单提交时的验证 javascript</returns>
34 public string GetValidatorOnSubmit()
35 {
36 StringBuilder validatorOnSubmit = new StringBuilder();
37
38 validatorOnSubmit.Append("<script language=javascript> ");
39 validatorOnSubmit.Append("function ValidatorOnSubmit() ");
40 validatorOnSubmit.Append("{ ");
41 validatorOnSubmit.Append(" var valid = true; ");
42 validatorOnSubmit.Append(" var i; ");
43 validatorOnSubmit.Append(" for (i = 0; i < Valid_Spans.length; i++) ");
44 validatorOnSubmit.Append(" { ");
45 validatorOnSubmit.Append(" var validFunction = Valid_Spans[i].evaluationfunction; ");
46 validatorOnSubmit.Append(" var inputID = Valid_Spans[i].controltovalidate; ");
47 validatorOnSubmit.Append(" var spanID = Valid_Spans[i].id; ");
48 validatorOnSubmit.Append(" if (!eval(validFunction + \"(inputID,spanID)\")) ");
49 validatorOnSubmit.Append(" { ");
50 validatorOnSubmit.Append(" valid = false; ");
51 validatorOnSubmit.Append(" } ");
52 validatorOnSubmit.Append(" } ");
53 validatorOnSubmit.Append(" return valid; ");
54 validatorOnSubmit.Append("} ");
55 validatorOnSubmit.Append("</script> ");
56
57 return validatorOnSubmit.ToString();
58
59 }
60
61 /**//// <summary>
62 /// 获取验证 Email 地址的 javascript
63 /// </summary>
64 /// <param name="errMessage">验证错误时的提示信息</param>
65 /// <returns>验证 javascript</returns>
66 public string GetIsEmailAddressScript(string errMessage)
67 {
68 StringBuilder emailAddressScript = new StringBuilder();
69
70 emailAddressScript.Append("<script language=javascript> ");
71 emailAddressScript.Append("function IsEmailAddress(inputID, spanID) ");
72 emailAddressScript.Append("{ ");
73 emailAddressScript.Append(" var emialAddress = document.getElementById(inputID).value; ");
74 emailAddressScript.Append(" " + @"if(emialAddress.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)!= -1)" + " ");
75 emailAddressScript.Append(" { ");
76 emailAddressScript.Append(" document.getElementById(spanID).innerText = ''; ");
77 emailAddressScript.Append(" return true; ");
78 emailAddressScript.Append(" } ");
79 emailAddressScript.Append(" else ");
80 emailAddressScript.Append(" { ");
81 emailAddressScript.Append(" alert('" + errMessage + "'); ");
82 emailAddressScript.Append(" document.getElementById(inputID).focus(); ");
83 emailAddressScript.Append(" return false; ");
84 emailAddressScript.Append(" } ");
85 emailAddressScript.Append("} ");
86 emailAddressScript.Append("</script> ");
87
88 return emailAddressScript.ToString();
89 }
90
91 /**//// <summary>
92 /// 获取验证不为空的 javascript
93 /// </summary>
94 /// <param name="errMessage">验证错误时的提示信息</param>
95 /// <returns>验证 javascript</returns>
96 public string GetIsRequiredFieldScript(string errMessage)
97 {
98 StringBuilder requiredFieldScript = new StringBuilder();
99
100 requiredFieldScript.Append("<script language=javascript> ");
101 requiredFieldScript.Append("function IsRequiredField(inputID, spanID) ");
102 requiredFieldScript.Append("{ ");
103 requiredFieldScript.Append(" var inputValue = document.getElementById(inputID).value; ");
104 requiredFieldScript.Append(" if (inputValue != \"\") ");
105 requiredFieldScript.Append(" { ");
106 requiredFieldScript.Append(" document.getElementById(spanID).innerText = ''; ");
107 requiredFieldScript.Append(" return true; ");
108 requiredFieldScript.Append(" } ");
109 requiredFieldScript.Append(" else ");
110 requiredFieldScript.Append(" { ");
111 requiredFieldScript.Append(" alert('" + errMessage + "'); ");
112 requiredFieldScript.Append(" document.getElementById(inputID).focus(); ");
113 requiredFieldScript.Append(" return false; ");
114 requiredFieldScript.Append(" } ");
115 requiredFieldScript.Append(" ");
116 requiredFieldScript.Append(" ");
117 requiredFieldScript.Append(" ");
118 requiredFieldScript.Append("} ");
119 requiredFieldScript.Append("</script> ");
120
121 return requiredFieldScript.ToString();
122 }
123
124 /**//// <summary>
125 /// 获取验证二个 input 值是否相等的 javascript
126 /// </summary>
127 /// <param name="errMessage">验证错误时的提示信息</param>
128 /// <returns>验证二个 input 值是否相等的 javascript</returns>
129 public string GetIsCompareScript(string errMessage)
130 {
131 StringBuilder compareScript = new StringBuilder();
132
133 compareScript.Append("<script language=javascript> ");
134 compareScript.Append("function IsCompare(inputID, spanID) ");
135 compareScript.Append("{ ");
136 compareScript.Append(" var inputValue = document.getElementById(inputID).value; ");
137 compareScript.Append(" var compareID = document.getElementById(spanID).controltocompare; ");
138
139 compareScript.Append(" if (compareID == \"\") ");
140 compareScript.Append(" { ");
141 compareScript.Append(" alert('没有设置与ID为' + inputID + '的输入框比较的对象!'); ");
142 compareScript.Append(" return; ");
143 compareScript.Append(" } ");
144
145 compareScript.Append(" if (document.getElementById(compareID) == null) ");
146 compareScript.Append(" { ");
147 compareScript.Append(" alert('与ID为' + inputID + '的输入框比较的对象不存在!'); ");
148 compareScript.Append(" return; ");
149 compareScript.Append(" } ");
150
151 compareScript.Append(" var compareValue = document.getElementById(compareID).value; ");
152
153 compareScript.Append(" if (inputValue == compareValue) ");
154 compareScript.Append(" { ");
155 compareScript.Append(" document.getElementById(spanID).innerText = ''; ");
156 compareScript.Append(" return true; ");
157 compareScript.Append(" } ");
158 compareScript.Append(" else ");
159 compareScript.Append(" { ");
160 compareScript.Append(" alert('" + errMessage + "'); ");
161 compareScript.Append(" document.getElementById(inputID).focus(); ");
162 compareScript.Append(" return false; ");
163 compareScript.Append(" } ");
164
165 compareScript.Append("} ");
166 compareScript.Append("</script> ");
167
168
169 return compareScript.ToString();
170 }
171
172 /**//// <summary>
173 /// 获取输入数据是否匹配指定的正则表达式的 javascript
174 /// </summary>
175 /// <param name="errMessage">验证错误时的提示信息</param>
176 /// <returns>输入数据是否匹配指定的正则表达式的 javascript</returns>
177 public string GetIsRegularExpressionScript(string errMessage)
178 {
179 StringBuilder regularExpressionScript = new StringBuilder();
180
181 regularExpressionScript.Append("<script language=javascript> ");
182 regularExpressionScript.Append("function IsRegularExpression(inputID, spanID) ");
183 regularExpressionScript.Append("{ ");
184 regularExpressionScript.Append(" var inputValue = document.getElementById(inputID).value; ");
185 regularExpressionScript.Append(" var regularExpression = document.getElementById(spanID).validRegularexpression ");
186 regularExpressionScript.Append(" if (typeof(eval(regularExpression)) == \"undefined\") ");
187 regularExpressionScript.Append(" { ");
188 regularExpressionScript.Append(" alert('正则表达式不正确!'); ");
189 regularExpressionScript.Append(" return; ");
190 regularExpressionScript.Append(" } ");
191
192 regularExpressionScript.Append(" if (eval(regularExpression).test(inputValue)) ");
193 regularExpressionScript.Append(" { ");
194 regularExpressionScript.Append(" document.getElementById(spanID).innerText = ''; ");
195 regularExpressionScript.Append(" return true; ");
196 regularExpressionScript.Append(" } ");
197 regularExpressionScript.Append(" else ");
198 regularExpressionScript.Append(" { ");
199 regularExpressionScript.Append(" alert('" + errMessage + "'); ");
200 regularExpressionScript.Append(" document.getElementById(inputID).focus(); ");
201 regularExpressionScript.Append(" return false; ");
202 regularExpressionScript.Append(" } ");
203
204 regularExpressionScript.Append("} ");
205 regularExpressionScript.Append("</script> ");
206
207 return regularExpressionScript.ToString();
208
209 }
210
211 /**//// <summary>
212 /// 获取验证日期和时间的 javascript
213 /// </summary>
214 /// <param name="errMessage">验证错误时的提示信息</param>
215 /// <returns>验证日期和时间的 javascript</returns>
216 public string GetIsDatetimeScript(string errMessage)
217 {
218 StringBuilder datetimeScript = new StringBuilder();
219
220 datetimeScript.Append("<script language=javascript> ");
221 datetimeScript.Append("function IsDateTime(inputID, spanID) ");
222 datetimeScript.Append("{ ");
223
224 datetimeScript.Append(" var inputValue = document.getElementById(inputID).value; ");
225
226 datetimeScript.Append(" if(inputValue == '') ");
227 datetimeScript.Append(" { ");
228// datetimeScript.Append(" document.getElementById(spanID).innerText = '请输入日期和时间'; ");
229 datetimeScript.Append(" alert('请输入日期和时间!')");
230 datetimeScript.Append(" return false; ");
231 datetimeScript.Append(" } ");
232
233 datetimeScript.Append(" var reg = /^(\\d{1,4})(-|\\/|\\.)(\\d{1,2})\\2(\\d{1,2}) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})$/; ");
234 datetimeScript.Append(" var r = inputValue.match(reg); ");
235
236 datetimeScript.Append(" if(r == null) ");
237 datetimeScript.Append(" { ");
238 datetimeScript.Append(" alert('" + errMessage + "'); ");
239 datetimeScript.Append(" document.getElementById(inputID).focus(); ");
240 datetimeScript.Append(" return false; ");
241 datetimeScript.Append(" } ");
242
243 datetimeScript.Append(" var d= new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]); ");
244
245 datetimeScript.Append(" if(r[1] != d.getFullYear() || r[3] != d.getMonth() + 1 || r[4] != d.getDate() || r[5] != d.getHours() || r[6] != d.getMinutes() || r[7] != d.getSeconds()) ");
246 datetimeScript.Append(" { ");
247 datetimeScript.Append(" alert('" + errMessage + "'); ");
248 datetimeScript.Append(" document.getElementById(inputID).focus(); ");
249 datetimeScript.Append(" return false; ");
250 datetimeScript.Append(" } ");
251
252 datetimeScript.Append(" if(r[3].length == 1) r[3] = '0' + r[3]; ");
253 datetimeScript.Append(" if(r[4].length == 1) r[4] = '0' + r[4]; ");
254 datetimeScript.Append(" if(r[5].length == 1) r[5] = '0' + r[5]; ");
255 datetimeScript.Append(" if(r[6].length == 1) r[6] = '0' + r[6]; ");
256 datetimeScript.Append(" if(r[7].length == 1) r[7] = '0' + r[7]; ");
257 datetimeScript.Append(" document.getElementById(inputID).value = r[1] + '-' + r[3] + '-' + r[4] + ' ' + r[5] + ':' + r[6] + ':' + r[7]; ");
258 datetimeScript.Append(" document.getElementById(spanID).innerText = ''; ");
259
260 datetimeScript.Append(" return true; ");
261 datetimeScript.Append("} ");
262 datetimeScript.Append("</script> ");
263
264 return datetimeScript.ToString();
265
266 }
267
268 /**//// <summary>
269 /// 获取验证日期的 javascript
270 /// </summary>
271 /// <param name="errMessage">验证错误时的提示信息</param>
272 /// <returns>验证日期的 javascript</returns>
273 public string GetIsDateScript(string errMessage)
274 {
275 StringBuilder dateScript = new StringBuilder();
276
277 dateScript.Append("<script language=javascript> ");
278 dateScript.Append("function IsDate(inputID, spanID) ");
279 dateScript.Append("{ ");
280
281 dateScript.Append(" var inputValue = document.getElementById(inputID).value; ");
282
283 dateScript.Append(" if(inputValue == '') ");
284 dateScript.Append(" { ");
285// dateScript.Append(" document.getElementById(spanID).innerText = '请输入日期'; ");
286 dateScript.Append(" alert('请输入日期!');");
287 dateScript.Append(" return false; ");
288 dateScript.Append(" } ");
289
290 dateScript.Append(" var reg = /^(\\d{1,4})(-|\\/|\\.)(\\d{1,2})\\2(\\d{1,2})$/; ");
291 dateScript.Append(" var r = inputValue.match(reg); ");
292
293 dateScript.Append(" if(r == null) ");
294 dateScript.Append(" { ");
295 dateScript.Append(" alert('" + errMessage + "'); ");
296 dateScript.Append(" document.getElementById(inputID).focus(); ");
297 dateScript.Append(" return false; ");
298 dateScript.Append(" } ");
299
300 dateScript.Append(" var d = new Date(r[1], r[3]-1, r[4]) ");
301
302 dateScript.Append(" if(r[1] != d.getFullYear() || r[3] != d.getMonth() + 1 || r[4] != d.getDate()) ");
303 dateScript.Append(" { ");
304 dateScript.Append(" alert('" + errMessage + "'); ");
305 dateScript.Append(" document.getElementById(inputID).focus(); ");
306 dateScript.Append(" return false; ");
307 dateScript.Append(" } ");
308
309
310 dateScript.Append(" if(r[3].length == 1) r[3] = '0' + r[3]; ");
311 dateScript.Append(" if(r[4].length == 1) r[4 ]= '0' + r[4]; ");
312
313
314 dateScript.Append(" var newValue = r[1] + '-' + r[3] + '-' + r[4]; ");
315 dateScript.Append(" document.getElementById(inputID).value = newValue; ");
316 dateScript.Append(" document.getElementById(spanID).innerText = ''; ");
317
318 dateScript.Append(" return true; ");
319 dateScript.Append("} ");
320 dateScript.Append("</script>");
321
322 return dateScript.ToString();
323 }
324
325
326 /**//// <summary>
327 /// 获取验证时间的 javascript
328 /// </summary>
329 /// <param name="errMessage">验证错误时的提示信息</param>
330 /// <returns>验证时间的 javascript</returns>
331 public string GetIsTimeScript(string errMessage)
332 {
333 StringBuilder timeScript = new StringBuilder();
334
335 timeScript.Append("<script language=javascript> ");
336 timeScript.Append("function IsTime(inputID, spanID) ");
337 timeScript.Append("{ ");
338 timeScript.Append(" var inputValue = document.getElementById(inputID).value; ");
339
340 timeScript.Append(" if(inputValue == '') ");
341 timeScript.Append(" { ");
342// timeScript.Append(" document.getElementById(spanID).innerText = '请输入时间'; ");
343 timeScript.Append(" alert('请输入时间');");
344 timeScript.Append(" return false; ");
345 timeScript.Append(" } ");
346
347
348 timeScript.Append(" var reg = /^(\\d{1,2}):(\\d{1,2}):(\\d{1,2})$/; ");
349 timeScript.Append(" var r = inputValue.match(reg); ");
350
351 timeScript.Append(" if(r == null) ");
352 timeScript.Append(" { ");
353 timeScript.Append(" alert('" + errMessage + "'); ");
354 timeScript.Append(" document.getElementById(inputID).focus(); ");
355
356 timeScript.Append(" return false; ");
357 timeScript.Append(" } ");
358
359 timeScript.Append(" if(r[1] > 24 || r[2] > 60 || r[3] > 60) ");
360 timeScript.Append(" { ");
361 timeScript.Append(" alert('" + errMessage + "'); ");
362 timeScript.Append(" document.getElementById(inputID).focus(); ");
363 timeScript.Append(" return false; ");
364 timeScript.Append(" } ");
365
366 timeScript.Append(" if(r[1].length == 1) r[1] = '0' + r[1]; ");
367 timeScript.Append(" if(r[2].length == 1) r[2] = '0' + r[2]; ");
368 timeScript.Append(" if(r[3].length == 1) r[3] = '0' + r[3]; ");
369
370 timeScript.Append(" document.getElementById(inputID).value = r[1] + ':' + r[2] + ':' + r[3]; ");
371 timeScript.Append(" document.getElementById(spanID).innerText = ''; ");
372
373 timeScript.Append(" return true; ");
374 timeScript.Append("} ");
375 timeScript.Append("</script> ");
376
377 return timeScript.ToString();
378 }
379
380
381 /**//// <summary>
382 /// 获取验证时间的 javascript
383 /// </summary>
384 /// <param name="errMessage">验证错误时的提示信息</param>
385 /// <returns>验证时间的 javascript</returns>
386 public string GetIsNumScript(string errMessage)
387 {
388 StringBuilder numScript = new StringBuilder();
389
390 numScript.Append("<script language=javascript> ");
391 numScript.Append("function IsNum(inputID, spanID) ");
392 numScript.Append("{ ");
393 numScript.Append(" var inputValue = document.getElementById(inputID).value; ");
394 numScript.Append(" if(isNaN(inputValue)) ");
395 numScript.Append(" { ");
396 numScript.Append(" alert('请输入数字!'); ");
397 numScript.Append(" document.getElementById(inputID).focus(); ");
398 numScript.Append(" return false; ");
399 numScript.Append(" } ");
400
401
402 numScript.Append(" return true; ");
403 numScript.Append("} ");
404 numScript.Append("</script> ");
405
406 return numScript.ToString();
407 }
408
409 #endregion
410 }
411}