[控件学习 - 2-2]Extra Valid TextBox[源码]

简介:

上次写了[控件学习-2]Extra Valid TextBox 学习了控件的如何扩展属性已及绑定脚本方法.
之后由于接了一个delphi的项目(其实我的主业是delphi),一直再加班赶工,没有再整理相关的一些代码出来.

觉得这个[控件学习-2]Extra Valid TextBox 还有很多可以改进的地方,先将最粗糙的代码帖上来,大家可以帮忙改进,或者提供更多的意见.

我也是在尝试修改各种不同需求来学习更多控件编写知识.

代码如下,需要的朋友可以看下,搬门弄斧了.......

KTextBox.cs

  1 None.gif using System;
  2 None.gif using System.Collections;
  3 None.gif using System.ComponentModel;
  4 None.gif using System.Drawing;
  5 None.gif using System.Web.UI;
  6 None.gif using System.Web.UI.WebControls;
  7 None.gif[assembly : TagPrefix("LangZi.WebControls", "KTX")]
  8 None.gif
  9 None.gif namespace LangZi.WebControls
 10 ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 12InBlock.gif    /// 功能:
 13InBlock.gif    ///        能在客户端自动对多种类型数据进行验证的 TextBox
 14InBlock.gif    ///        
 15InBlock.gif    ///        可验证的类型为:
 16InBlock.gif    ///            1.不为空
 17InBlock.gif    ///            2.和另一个输入框比较,看值是否相等
 18InBlock.gif    ///            3.日期时间
 19InBlock.gif    ///            4.日期
 20InBlock.gif    ///            5.时间
 21InBlock.gif    ///            6.Email地址
 22InBlock.gif    ///            
 23InBlock.gif    ///        内置的几种常用正则表达式验证:
 24InBlock.gif    ///            1.
 25InBlock.gif    ///            2.
 26InBlock.gif    ///            
 27InBlock.gif    ///        另外,如果设置为不进行任何验证,功能和普通的 TextBox 一样
 28InBlock.gif    ///            
 29InBlock.gif    ///    
 30InBlock.gif    /// 修改记录:
 31InBlock.gif    ///  
 32InBlock.gif    ///     日期            修改人        主要修改说明
 33InBlock.gif    ///     
 34InBlock.gif    ///        2005.9.8        陈达艺        新建
 35InBlock.gif    ///        
 36InBlock.gif    ///        
 37ExpandedSubBlockEnd.gif    /// </summary>    

 38InBlock.gif    [DefaultProperty("ValidType")]
 39InBlock.gif    [ToolboxData("<{0}:KTextBox runat=server></{0}:KTextBox>")]
 40InBlock.gif    public class KTextBox : System.Web.UI.WebControls.TextBox, INamingContainer
 41ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 42ContractedSubBlock.gifExpandedSubBlockStart.gif        私有成员变量#region 私有成员变量
 43InBlock.gif
 44InBlock.gif        //枚举可以验证的类型
 45InBlock.gif        public enum AvailableType
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 47InBlock.gif            NoSet, //不进行验证,功能和普通的 TextBox 一样
 48InBlock.gif            EmailAddress, //Email地址
 49InBlock.gif            RequiredField, //不为空
 50InBlock.gif            Compare, //和另一个控件比较,看值是否相等
 51InBlock.gif            RegularExpression, //正则表达式
 52InBlock.gif            DateTime, //日期时间
 53InBlock.gif            Date, //日期
 54InBlock.gif            Time, //时间
 55InBlock.gif            Num //数字
 56ExpandedSubBlockEnd.gif        }

 57InBlock.gif
 58InBlock.gif        //验证时间
 59InBlock.gif        public enum ValidTime
 60ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 61InBlock.gif            OnBlur, //失去焦点时进行验证
 62InBlock.gif            OnSubmit, //页面提交时进行验证
 63InBlock.gif            All //失去焦点和页面提交时都进行验证
 64ExpandedSubBlockEnd.gif        }

 65InBlock.gif
 66InBlock.gif        private Label lblMessage;
 67InBlock.gif        private AvailableType _validType;
 68InBlock.gif        private string _errorMessage;
 69InBlock.gif        private ValidTime _validMethod;
 70InBlock.gif        private string _compareControl;
 71InBlock.gif        private string _validRegularExpression;
 72InBlock.gif
 73ExpandedSubBlockEnd.gif        #endregion
 私有成员变量
 74InBlock.gif
 75ContractedSubBlock.gifExpandedSubBlockStart.gif        公有属性#region 公有属性
 76InBlock.gif
 77ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 78InBlock.gif        /// Set/Get trimmed text of textbox
 79ExpandedSubBlockEnd.gif        /// </summary>

 80InBlock.gif        [Browsable(false)]
 81InBlock.gif        [Description("Set/Get trimmed text of textbox")]
 82InBlock.gif        public string TextTrimed
 83ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 84ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn this.Text.Trim(); }
 85ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis.Text = value.Trim(); }
 86ExpandedSubBlockEnd.gif        }

 87InBlock.gif
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 89InBlock.gif        /// Get/Set value datetime of textbox
 90ExpandedSubBlockEnd.gif        /// </summary>

 91InBlock.gif        [Browsable(false)]
 92InBlock.gif        [Description("Get/Set value datetime of textbox")]
 93InBlock.gif        public DateTime DateTimeValue
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 95InBlock.gif            get
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 97InBlock.gif                try
 98ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 99InBlock.gif                    DateTime dtm = DateTime.Parse(this.TextTrimed);
100InBlock.gif                    return dtm;
101ExpandedSubBlockEnd.gif                }

102InBlock.gif                catch (Exception exp)
103ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
104InBlock.gif                    throw exp;
105ExpandedSubBlockEnd.gif                }

106ExpandedSubBlockEnd.gif            }

107ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis.Text = value.ToString(); }
108ExpandedSubBlockEnd.gif        }

109InBlock.gif
110InBlock.gif
111ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
112InBlock.gif        /// Check whether the textbox is empty, if it is, return true
113ExpandedSubBlockEnd.gif        /// </summary>

114InBlock.gif        [Browsable(false)]
115InBlock.gif        [Description("Check whether the textbox is empty, if it is, return true")]
116InBlock.gif        public bool IsEmpty
117ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
118ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn this.Text.Length == 0; }
119ExpandedSubBlockEnd.gif        }

120InBlock.gif
121ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
122InBlock.gif        /// Get whether the value of textbox is the type int
123ExpandedSubBlockEnd.gif        /// </summary>

124InBlock.gif        [Browsable(false)]
125InBlock.gif        [Description("Get whether the value of textbox is the type int")]
126InBlock.gif        public bool IsInt
127ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
128InBlock.gif            get
129ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
130InBlock.gif                try
131ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
132InBlock.gif                    int v = int.Parse(this.TextTrimed);
133InBlock.gif                    return true;
134ExpandedSubBlockEnd.gif                }

135InBlock.gif                catch
136ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
137InBlock.gif                    return false;
138ExpandedSubBlockEnd.gif                }

139ExpandedSubBlockEnd.gif            }

140ExpandedSubBlockEnd.gif        }

141InBlock.gif
142ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
143InBlock.gif        /// 获取和设置进行验证的数据类型
144ExpandedSubBlockEnd.gif        /// </summary>

145InBlock.gif        [Category("验证")]
146InBlock.gif        [Browsable(true)]
147InBlock.gif        [Description("进行验证的数据类型")]
148InBlock.gif        public AvailableType ValidType
149ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
150ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _validType; }
151ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _validType = value; }
152ExpandedSubBlockEnd.gif        }

153InBlock.gif
154ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
155InBlock.gif        /// 获取和设置验证错误时的提示信息
156ExpandedSubBlockEnd.gif        /// </summary>

157InBlock.gif        [Category("验证")]
158InBlock.gif        [Browsable(true)]
159InBlock.gif        [Description("验证错误时的提示信息")]
160InBlock.gif        public string ErrorMessage
161ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
162ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _errorMessage; }
163ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _errorMessage = value; }
164ExpandedSubBlockEnd.gif        }

165InBlock.gif
166ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
167InBlock.gif        /// 获取和设置触发验证的方法
168ExpandedSubBlockEnd.gif        /// </summary>

169InBlock.gif        [Category("验证")]
170InBlock.gif        [Browsable(true)]
171InBlock.gif        [Description("触发验证的方法")]
172InBlock.gif        public ValidTime ValidMethod
173ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
174ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _validMethod; }
175ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _validMethod = value; }
176ExpandedSubBlockEnd.gif        }

177InBlock.gif
178ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
179InBlock.gif        /// 获取和设置进行值比较的控件
180ExpandedSubBlockEnd.gif        /// </summary>

181InBlock.gif        [Category("验证")]
182InBlock.gif        [Browsable(true)]
183InBlock.gif        [TypeConverter(typeof (CompareControlConverter))]
184InBlock.gif        [Description("与此控件进行值比较的控件")]
185InBlock.gif        public string CompareControl
186ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
187ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _compareControl; }
188ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _compareControl = value; }
189ExpandedSubBlockEnd.gif        }

190InBlock.gif
191ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
192InBlock.gif        /// 获取和设置进行验证的正则表达式
193ExpandedSubBlockEnd.gif        /// </summary>

194InBlock.gif        [Category("验证")]
195InBlock.gif        [Browsable(true)]
196InBlock.gif        [Description("进行验证的正则表达式")]
197InBlock.gif        public string ValidRegularExpression
198ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
199ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _validRegularExpression; }
200ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _validRegularExpression = value; }
201ExpandedSubBlockEnd.gif        }

202InBlock.gif
203ExpandedSubBlockEnd.gif        #endregion
 公有属性
204InBlock.gif
205ContractedSubBlock.gifExpandedSubBlockStart.gif        创建控件层次结构#region 创建控件层次结构
206InBlock.gif
207ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
208InBlock.gif        /// 创建子控件
209ExpandedSubBlockEnd.gif        /// </summary>

210InBlock.gif        protected override void CreateChildControls()
211ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
212InBlock.gif            base.CreateChildControls();
213InBlock.gif
214InBlock.gif            lblMessage = new Label();
215InBlock.gif            lblMessage.ID = "lblMessage";
216InBlock.gif            lblMessage.ControlStyle.ForeColor = Color.Red;
217InBlock.gif            lblMessage.EnableViewState = true;
218InBlock.gif
219InBlock.gif            AddAttributesInf();
220InBlock.gif
221InBlock.gif
222InBlock.gif            this.Controls.Add(lblMessage);
223ExpandedSubBlockEnd.gif        }

224InBlock.gif
225ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
226InBlock.gif        /// 向 lblMessage 添加一些名值对信息
227ExpandedSubBlockEnd.gif        /// </summary>

228InBlock.gif        private void AddAttributesInf()
229ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
230InBlock.gif            //根据验证的类型这些名值对信息不相同
231InBlock.gif            switch (ValidType)
232ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
233InBlock.gif                case AvailableType.NoSet:
234InBlock.gif                    break;
235InBlock.gif                case AvailableType.EmailAddress:
236InBlock.gif                    lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
237InBlock.gif                    lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
238InBlock.gif                    break;
239InBlock.gif                case AvailableType.RequiredField:
240InBlock.gif                    lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
241InBlock.gif                    lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
242InBlock.gif                    break;
243InBlock.gif                case AvailableType.Compare:
244InBlock.gif                    lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
245InBlock.gif                    lblMessage.Attributes.Add("controltocompare", CompareControl);
246InBlock.gif                    lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
247InBlock.gif                    break;
248InBlock.gif                case AvailableType.RegularExpression:
249InBlock.gif                    lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
250InBlock.gif                    lblMessage.Attributes.Add("validRegularexpression", ValidRegularExpression);
251InBlock.gif                    lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
252InBlock.gif                    break;
253InBlock.gif                case AvailableType.DateTime:
254InBlock.gif                    lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
255InBlock.gif                    lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
256InBlock.gif                    break;
257InBlock.gif                case AvailableType.Date:
258InBlock.gif                    lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
259InBlock.gif                    lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
260InBlock.gif                    break;
261InBlock.gif                case AvailableType.Time:
262InBlock.gif                    lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
263InBlock.gif                    lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
264InBlock.gif                    break;
265InBlock.gif                case AvailableType.Num:
266InBlock.gif                    lblMessage.Attributes.Add("controltovalidate", this.UniqueID);
267InBlock.gif                    lblMessage.Attributes.Add("evaluationfunction", "Is" + ValidType.ToString());
268InBlock.gif                    break;
269InBlock.gif
270ExpandedSubBlockEnd.gif            }

271InBlock.gif
272ExpandedSubBlockEnd.gif        }

273InBlock.gif
274ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
275InBlock.gif        /// 由于继承于 TextBox 控件,RenderContents 方法不会自动被系统调用,
276InBlock.gif        /// 所以在 RenderEndTag 方法中调用 RenderContents ,用来呈现 CreateChildControls 方法中添加的控件,
277InBlock.gif        /// 如果不这样,CreateChildControls 方法中添加的控件不会呈现在客户端
278InBlock.gif        /// </summary>
279ExpandedSubBlockEnd.gif        /// <param name="writer"></param>

280InBlock.gif        public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
281ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
282InBlock.gif            base.RenderEndTag(writer);
283InBlock.gif            this.RenderContents(writer);
284ExpandedSubBlockEnd.gif        }

285InBlock.gif
286ExpandedSubBlockEnd.gif        #endregion
 创建控件层次结构
287InBlock.gif
288ContractedSubBlock.gifExpandedSubBlockStart.gif        注册 javascript#region 注册 javascript
289InBlock.gif
290ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
291InBlock.gif        /// 预呈现,在这里向客户端注册 javascript 块
292InBlock.gif        /// </summary>
293ExpandedSubBlockEnd.gif        /// <param name="e"></param>

294InBlock.gif        protected override void OnPreRender(EventArgs e)
295ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
296InBlock.gif            base.OnPreRender(e);
297InBlock.gif
298InBlock.gif            //设置为进行验证时才向客户端注册 javascript
299InBlock.gif            if (ValidType != AvailableType.NoSet)
300ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
301InBlock.gif                RegisterClientScript(ValidType);
302InBlock.gif
303InBlock.gif                //注册客户端对象数组
304InBlock.gif                Page.RegisterArrayDeclaration("Valid_Spans", "document.getElementById(\"" + this.UniqueID + "_lblMessage\")");
305InBlock.gif                //注册页面提交时的 javascript
306InBlock.gif                Page.RegisterOnSubmitStatement("ValidatorOnSubmit", "return ValidatorOnSubmit();");
307ExpandedSubBlockEnd.gif            }

308ExpandedSubBlockEnd.gif        }

309InBlock.gif
310ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
311InBlock.gif        /// 注册客户端 javascript
312InBlock.gif        /// </summary>
313ExpandedSubBlockEnd.gif        /// <param name="vaildType">验证类型</param>

314InBlock.gif        private void RegisterClientScript(AvailableType vaildType)
315ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
316InBlock.gif            string registerKey = vaildType.ToString();
317InBlock.gif
318InBlock.gif            ClientScript clientScript = new ClientScript();
319InBlock.gif
320InBlock.gif
321InBlock.gif            //注册表单提交时的验证 javascript
322InBlock.gif            if ((ValidMethod == ValidTime.All) || (ValidMethod == ValidTime.OnSubmit))
323ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
324InBlock.gif                if (!Page.IsClientScriptBlockRegistered("validatorOnSubmit"))
325ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
326InBlock.gif                    Page.RegisterClientScriptBlock("validatorOnSubmit", clientScript.GetValidatorOnSubmit());
327ExpandedSubBlockEnd.gif                }

328ExpandedSubBlockEnd.gif            }

329InBlock.gif
330InBlock.gif
331InBlock.gif            //注册对指定类型数据进行验证的 javascript
332InBlock.gif            if (!Page.IsClientScriptBlockRegistered(registerKey))
333ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
334InBlock.gif                Page.RegisterClientScriptBlock(registerKey, GetClientScript(vaildType));
335ExpandedSubBlockEnd.gif            }

336InBlock.gif
337InBlock.gif            //如果设置为在失去焦点的时候进行验证,注册 onBlur 事件
338InBlock.gif            if ((ValidMethod == ValidTime.All) || (ValidMethod == ValidTime.OnBlur))
339ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
340InBlock.gif                //处理事件的函数
341InBlock.gif                string validFunction = "Is" + ValidType.ToString() + "('" + this.UniqueID + "', '" + this.UniqueID + "_lblMessage')";
342InBlock.gif
343InBlock.gif                base.Attributes.Add("onBlur", validFunction);
344ExpandedSubBlockEnd.gif            }

345ExpandedSubBlockEnd.gif        }

346InBlock.gif
347ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
348InBlock.gif        /// 根据需要验证的数据类型获取验证的 javascript
349InBlock.gif        /// </summary>
350InBlock.gif        /// <param name="vaildType">需要验证的数据类型</param>
351ExpandedSubBlockEnd.gif        /// <returns>javascript</returns>

352InBlock.gif        private string GetClientScript(AvailableType vaildType)
353ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
354InBlock.gif            string script = "";
355InBlock.gif
356InBlock.gif            //实例化创建脚本的对象
357InBlock.gif            ClientScript clientScript = new ClientScript();
358InBlock.gif
359InBlock.gif            //根据验证类型获取验证 javascript
360InBlock.gif            switch (vaildType)
361ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
362InBlock.gif                case AvailableType.EmailAddress:
363InBlock.gif                    script = clientScript.GetIsEmailAddressScript(ErrorMessage);
364InBlock.gif                    break;
365InBlock.gif                case AvailableType.RequiredField:
366InBlock.gif                    script = clientScript.GetIsRequiredFieldScript(ErrorMessage);
367InBlock.gif                    break;
368InBlock.gif                case AvailableType.Compare:
369InBlock.gif                    script = clientScript.GetIsCompareScript(ErrorMessage);
370InBlock.gif                    break;
371InBlock.gif                case AvailableType.RegularExpression:
372InBlock.gif                    script = clientScript.GetIsRegularExpressionScript(ErrorMessage);
373InBlock.gif                    break;
374InBlock.gif                case AvailableType.DateTime:
375InBlock.gif                    script = clientScript.GetIsDatetimeScript(ErrorMessage);
376InBlock.gif                    break;
377InBlock.gif                case AvailableType.Date:
378InBlock.gif                    script = clientScript.GetIsDateScript(ErrorMessage);
379InBlock.gif                    break;
380InBlock.gif                case AvailableType.Time:
381InBlock.gif                    script = clientScript.GetIsTimeScript(ErrorMessage);
382InBlock.gif                    break;
383InBlock.gif                case AvailableType.Num:
384InBlock.gif                    script = clientScript.GetIsNumScript(ErrorMessage);
385InBlock.gif                    break;
386InBlock.gif
387ExpandedSubBlockEnd.gif            }

388InBlock.gif
389InBlock.gif            return script;
390InBlock.gif
391ExpandedSubBlockEnd.gif        }

392InBlock.gif
393ExpandedSubBlockEnd.gif        #endregion
 注册 javascript
394ExpandedSubBlockEnd.gif    }

395InBlock.gif
396ContractedSubBlock.gifExpandedSubBlockStart.gif    class CompareControlConverter : TypeConverter#region class CompareControlConverter : TypeConverter
397InBlock.gif
398ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
399InBlock.gif    /// 对 KTextBox 控件 CompareControl 属性值进行类型转换的类
400ExpandedSubBlockEnd.gif    /// </summary>

401InBlock.gif    public class CompareControlConverter : TypeConverter
402ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
403InBlock.gif        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
404ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
405InBlock.gif            return true;
406ExpandedSubBlockEnd.gif        }

407InBlock.gif
408InBlock.gif        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
409ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
410InBlock.gif            return true;
411ExpandedSubBlockEnd.gif        }

412InBlock.gif
413InBlock.gif        public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
414ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
415InBlock.gif            ArrayList controlList = new ArrayList();
416InBlock.gif
417InBlock.gif            foreach (IComponent comp in context.Container.Components)
418ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
419InBlock.gif                if (comp is KTextBox)
420ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
421InBlock.gif                    controlList.Add(((KTextBox) comp).ID);
422ExpandedSubBlockEnd.gif                }

423ExpandedSubBlockEnd.gif            }

424InBlock.gif
425InBlock.gif            return new StandardValuesCollection(controlList);
426ExpandedSubBlockEnd.gif        }

427InBlock.gif
428ExpandedSubBlockEnd.gif    }

429InBlock.gif
430ExpandedSubBlockEnd.gif    #endregion
 class CompareControlConverter : TypeConverter
431ExpandedBlockEnd.gif}


脚本生成:
  1 None.gif using System.Text;
  2 None.gif
  3 None.gif namespace LangZi.WebControls
  4 ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
  5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  6InBlock.gif    /// 功能:
  7InBlock.gif    ///        生成客户端脚本
  8InBlock.gif    ///            
  9InBlock.gif    ///    
 10InBlock.gif    /// 修改记录:
 11InBlock.gif    ///  
 12InBlock.gif    ///     日期            修改人        主要修改说明
 13InBlock.gif    ///     
 14InBlock.gif    ///        2005.9.8        陈达艺        新建
 15InBlock.gif    ///        
 16InBlock.gif    ///        
 17ExpandedSubBlockEnd.gif    /// </summary>    

 18InBlock.gif    public class ClientScript
 19ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 20ContractedSubBlock.gifExpandedSubBlockStart.gif        Constructor#region Constructor
 21InBlock.gif
 22InBlock.gif        public ClientScript()
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 24ExpandedSubBlockEnd.gif        }

 25InBlock.gif
 26ExpandedSubBlockEnd.gif        #endregion

 27InBlock.gif
 28ContractedSubBlock.gifExpandedSubBlockStart.gif        KTextBox#region KTextBox
 29InBlock.gif
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 31InBlock.gif        /// 获取表单提交时的验证 javascript
 32InBlock.gif        /// </summary>
 33ExpandedSubBlockEnd.gif        /// <returns>表单提交时的验证 javascript</returns>

 34InBlock.gif        public string GetValidatorOnSubmit()
 35ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 36InBlock.gif            StringBuilder validatorOnSubmit = new StringBuilder();
 37InBlock.gif
 38InBlock.gif            validatorOnSubmit.Append("<script language=javascript> ");
 39InBlock.gif            validatorOnSubmit.Append("function ValidatorOnSubmit() ");
 40InBlock.gif            validatorOnSubmit.Append("{ ");
 41InBlock.gif            validatorOnSubmit.Append(" var valid = true; ");
 42InBlock.gif            validatorOnSubmit.Append(" var i; ");
 43InBlock.gif            validatorOnSubmit.Append(" for (i = 0; i < Valid_Spans.length; i++) ");
 44InBlock.gif            validatorOnSubmit.Append(" { ");
 45InBlock.gif            validatorOnSubmit.Append("  var validFunction = Valid_Spans[i].evaluationfunction; ");
 46InBlock.gif            validatorOnSubmit.Append("  var inputID = Valid_Spans[i].controltovalidate; ");
 47InBlock.gif            validatorOnSubmit.Append("  var spanID = Valid_Spans[i].id; ");
 48InBlock.gif            validatorOnSubmit.Append("  if (!eval(validFunction + \"(inputID,spanID)\")) ");
 49InBlock.gif            validatorOnSubmit.Append("  { ");
 50InBlock.gif            validatorOnSubmit.Append("   valid = false; ");
 51InBlock.gif            validatorOnSubmit.Append("  } ");
 52InBlock.gif            validatorOnSubmit.Append(" } ");
 53InBlock.gif            validatorOnSubmit.Append(" return valid; ");
 54InBlock.gif            validatorOnSubmit.Append("} ");
 55InBlock.gif            validatorOnSubmit.Append("</script> ");
 56InBlock.gif
 57InBlock.gif            return validatorOnSubmit.ToString();
 58InBlock.gif
 59ExpandedSubBlockEnd.gif        }

 60InBlock.gif
 61ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 62InBlock.gif        /// 获取验证 Email 地址的 javascript
 63InBlock.gif        /// </summary>
 64InBlock.gif        /// <param name="errMessage">验证错误时的提示信息</param>
 65ExpandedSubBlockEnd.gif        /// <returns>验证 javascript</returns>

 66InBlock.gif        public string GetIsEmailAddressScript(string errMessage)
 67ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 68InBlock.gif            StringBuilder emailAddressScript = new StringBuilder();
 69InBlock.gif
 70InBlock.gif            emailAddressScript.Append("<script language=javascript> ");
 71InBlock.gif            emailAddressScript.Append("function IsEmailAddress(inputID, spanID) ");
 72InBlock.gif            emailAddressScript.Append("{ ");
 73InBlock.gif            emailAddressScript.Append(" var emialAddress = document.getElementById(inputID).value; ");
 74InBlock.gif            emailAddressScript.Append(" " + @"if(emialAddress.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)!= -1)" + " ");
 75InBlock.gif            emailAddressScript.Append(" { ");
 76InBlock.gif            emailAddressScript.Append("  document.getElementById(spanID).innerText = ''; ");
 77InBlock.gif            emailAddressScript.Append("  return true; ");
 78InBlock.gif            emailAddressScript.Append(" } ");
 79InBlock.gif            emailAddressScript.Append(" else ");
 80InBlock.gif            emailAddressScript.Append(" { ");
 81InBlock.gif            emailAddressScript.Append("  alert('" + errMessage + "'); ");
 82InBlock.gif            emailAddressScript.Append("  document.getElementById(inputID).focus(); ");
 83InBlock.gif            emailAddressScript.Append("  return false; ");
 84InBlock.gif            emailAddressScript.Append(" } ");
 85InBlock.gif            emailAddressScript.Append("} ");
 86InBlock.gif            emailAddressScript.Append("</script> ");
 87InBlock.gif
 88InBlock.gif            return emailAddressScript.ToString();
 89ExpandedSubBlockEnd.gif        }

 90InBlock.gif
 91ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 92InBlock.gif        /// 获取验证不为空的 javascript
 93InBlock.gif        /// </summary>
 94InBlock.gif        /// <param name="errMessage">验证错误时的提示信息</param>
 95ExpandedSubBlockEnd.gif        /// <returns>验证 javascript</returns>

 96InBlock.gif        public string GetIsRequiredFieldScript(string errMessage)
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 98InBlock.gif            StringBuilder requiredFieldScript = new StringBuilder();
 99InBlock.gif
100InBlock.gif            requiredFieldScript.Append("<script language=javascript> ");
101InBlock.gif            requiredFieldScript.Append("function IsRequiredField(inputID, spanID) ");
102InBlock.gif            requiredFieldScript.Append("{ ");
103InBlock.gif            requiredFieldScript.Append(" var inputValue = document.getElementById(inputID).value; ");
104InBlock.gif            requiredFieldScript.Append(" if (inputValue != \"\") ");
105InBlock.gif            requiredFieldScript.Append(" { ");
106InBlock.gif            requiredFieldScript.Append("  document.getElementById(spanID).innerText = ''; ");
107InBlock.gif            requiredFieldScript.Append("  return true; ");
108InBlock.gif            requiredFieldScript.Append(" } ");
109InBlock.gif            requiredFieldScript.Append(" else ");
110InBlock.gif            requiredFieldScript.Append(" { ");
111InBlock.gif            requiredFieldScript.Append("  alert('" + errMessage + "'); ");
112InBlock.gif            requiredFieldScript.Append("  document.getElementById(inputID).focus(); ");
113InBlock.gif            requiredFieldScript.Append("  return false; ");
114InBlock.gif            requiredFieldScript.Append(" } ");
115InBlock.gif            requiredFieldScript.Append(" ");
116InBlock.gif            requiredFieldScript.Append(" ");
117InBlock.gif            requiredFieldScript.Append(" ");
118InBlock.gif            requiredFieldScript.Append("} ");
119InBlock.gif            requiredFieldScript.Append("</script> ");
120InBlock.gif
121InBlock.gif            return requiredFieldScript.ToString();
122ExpandedSubBlockEnd.gif        }

123InBlock.gif
124ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
125InBlock.gif        /// 获取验证二个 input 值是否相等的 javascript
126InBlock.gif        /// </summary>
127InBlock.gif        /// <param name="errMessage">验证错误时的提示信息</param>
128ExpandedSubBlockEnd.gif        /// <returns>验证二个 input 值是否相等的 javascript</returns>

129InBlock.gif        public string GetIsCompareScript(string errMessage)
130ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
131InBlock.gif            StringBuilder compareScript = new StringBuilder();
132InBlock.gif
133InBlock.gif            compareScript.Append("<script language=javascript> ");
134InBlock.gif            compareScript.Append("function IsCompare(inputID, spanID) ");
135InBlock.gif            compareScript.Append("{ ");
136InBlock.gif            compareScript.Append(" var inputValue = document.getElementById(inputID).value; ");
137InBlock.gif            compareScript.Append(" var compareID = document.getElementById(spanID).controltocompare; ");
138InBlock.gif
139InBlock.gif            compareScript.Append(" if (compareID == \"\") ");
140InBlock.gif            compareScript.Append(" { ");
141InBlock.gif            compareScript.Append("  alert('没有设置与ID为' + inputID + '的输入框比较的对象!'); ");
142InBlock.gif            compareScript.Append("  return; ");
143InBlock.gif            compareScript.Append(" } ");
144InBlock.gif
145InBlock.gif            compareScript.Append(" if (document.getElementById(compareID) == null) ");
146InBlock.gif            compareScript.Append(" { ");
147InBlock.gif            compareScript.Append("  alert('与ID为' + inputID + '的输入框比较的对象不存在!'); ");
148InBlock.gif            compareScript.Append("  return; ");
149InBlock.gif            compareScript.Append(" } ");
150InBlock.gif
151InBlock.gif            compareScript.Append(" var compareValue = document.getElementById(compareID).value; ");
152InBlock.gif
153InBlock.gif            compareScript.Append(" if (inputValue == compareValue) ");
154InBlock.gif            compareScript.Append(" { ");
155InBlock.gif            compareScript.Append("  document.getElementById(spanID).innerText = ''; ");
156InBlock.gif            compareScript.Append("  return true; ");
157InBlock.gif            compareScript.Append(" } ");
158InBlock.gif            compareScript.Append(" else ");
159InBlock.gif            compareScript.Append(" { ");
160InBlock.gif            compareScript.Append("  alert('" + errMessage + "'); ");
161InBlock.gif            compareScript.Append("  document.getElementById(inputID).focus(); ");
162InBlock.gif            compareScript.Append("  return false; ");
163InBlock.gif            compareScript.Append(" } ");
164InBlock.gif
165InBlock.gif            compareScript.Append("} ");
166InBlock.gif            compareScript.Append("</script> ");
167InBlock.gif
168InBlock.gif
169InBlock.gif            return compareScript.ToString();
170ExpandedSubBlockEnd.gif        }

171InBlock.gif
172ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
173InBlock.gif        /// 获取输入数据是否匹配指定的正则表达式的 javascript
174InBlock.gif        /// </summary>
175InBlock.gif        /// <param name="errMessage">验证错误时的提示信息</param>
176ExpandedSubBlockEnd.gif        /// <returns>输入数据是否匹配指定的正则表达式的 javascript</returns>

177InBlock.gif        public string GetIsRegularExpressionScript(string errMessage)
178ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
179InBlock.gif            StringBuilder regularExpressionScript = new StringBuilder();
180InBlock.gif
181InBlock.gif            regularExpressionScript.Append("<script language=javascript> ");
182InBlock.gif            regularExpressionScript.Append("function IsRegularExpression(inputID, spanID) ");
183InBlock.gif            regularExpressionScript.Append("{ ");
184InBlock.gif            regularExpressionScript.Append(" var inputValue = document.getElementById(inputID).value; ");
185InBlock.gif            regularExpressionScript.Append(" var regularExpression = document.getElementById(spanID).validRegularexpression ");
186InBlock.gif            regularExpressionScript.Append(" if (typeof(eval(regularExpression)) == \"undefined\") ");
187InBlock.gif            regularExpressionScript.Append(" { ");
188InBlock.gif            regularExpressionScript.Append("  alert('正则表达式不正确!'); ");
189InBlock.gif            regularExpressionScript.Append("  return; ");
190InBlock.gif            regularExpressionScript.Append(" } ");
191InBlock.gif
192InBlock.gif            regularExpressionScript.Append(" if (eval(regularExpression).test(inputValue)) ");
193InBlock.gif            regularExpressionScript.Append(" { ");
194InBlock.gif            regularExpressionScript.Append("  document.getElementById(spanID).innerText = ''; ");
195InBlock.gif            regularExpressionScript.Append("  return true; ");
196InBlock.gif            regularExpressionScript.Append(" } ");
197InBlock.gif            regularExpressionScript.Append(" else ");
198InBlock.gif            regularExpressionScript.Append(" { ");
199InBlock.gif            regularExpressionScript.Append("  alert('" + errMessage + "'); ");
200InBlock.gif            regularExpressionScript.Append("  document.getElementById(inputID).focus(); ");
201InBlock.gif            regularExpressionScript.Append("  return false; ");
202InBlock.gif            regularExpressionScript.Append(" } ");
203InBlock.gif
204InBlock.gif            regularExpressionScript.Append("} ");
205InBlock.gif            regularExpressionScript.Append("</script> ");
206InBlock.gif
207InBlock.gif            return regularExpressionScript.ToString();
208InBlock.gif
209ExpandedSubBlockEnd.gif        }

210InBlock.gif
211ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
212InBlock.gif        /// 获取验证日期和时间的 javascript
213InBlock.gif        /// </summary>
214InBlock.gif        /// <param name="errMessage">验证错误时的提示信息</param>
215ExpandedSubBlockEnd.gif        /// <returns>验证日期和时间的 javascript</returns>

216InBlock.gif        public string GetIsDatetimeScript(string errMessage)
217ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
218InBlock.gif            StringBuilder datetimeScript = new StringBuilder();
219InBlock.gif
220InBlock.gif            datetimeScript.Append("<script language=javascript> ");
221InBlock.gif            datetimeScript.Append("function IsDateTime(inputID, spanID) ");
222InBlock.gif            datetimeScript.Append("{ ");
223InBlock.gif
224InBlock.gif            datetimeScript.Append(" var inputValue = document.getElementById(inputID).value; ");
225InBlock.gif
226InBlock.gif            datetimeScript.Append(" if(inputValue == '') ");
227InBlock.gif            datetimeScript.Append(" { ");
228InBlock.gif//            datetimeScript.Append("  document.getElementById(spanID).innerText = '请输入日期和时间'; ");
229InBlock.gif            datetimeScript.Append("  alert('请输入日期和时间!')");
230InBlock.gif            datetimeScript.Append("  return false; ");
231InBlock.gif            datetimeScript.Append(" } ");
232InBlock.gif
233InBlock.gif            datetimeScript.Append(" var reg = /^(\\d{1,4})(-|\\/|\\.)(\\d{1,2})\\2(\\d{1,2}) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})$/; ");
234InBlock.gif            datetimeScript.Append(" var r = inputValue.match(reg); ");
235InBlock.gif
236InBlock.gif            datetimeScript.Append(" if(r == null) ");
237InBlock.gif            datetimeScript.Append(" { ");
238InBlock.gif            datetimeScript.Append("  alert('" + errMessage + "'); ");
239InBlock.gif            datetimeScript.Append("  document.getElementById(inputID).focus(); ");
240InBlock.gif            datetimeScript.Append("  return false; ");
241InBlock.gif            datetimeScript.Append(" } ");
242InBlock.gif
243InBlock.gif            datetimeScript.Append(" var d= new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]); ");
244InBlock.gif
245InBlock.gif            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()) ");
246InBlock.gif            datetimeScript.Append(" { ");
247InBlock.gif            datetimeScript.Append("  alert('" + errMessage + "'); ");
248InBlock.gif            datetimeScript.Append("  document.getElementById(inputID).focus(); ");
249InBlock.gif            datetimeScript.Append("  return false; ");
250InBlock.gif            datetimeScript.Append(" } ");
251InBlock.gif
252InBlock.gif            datetimeScript.Append(" if(r[3].length == 1) r[3] = '0' + r[3]; ");
253InBlock.gif            datetimeScript.Append(" if(r[4].length == 1) r[4] = '0' + r[4]; ");
254InBlock.gif            datetimeScript.Append(" if(r[5].length == 1) r[5] = '0' + r[5]; ");
255InBlock.gif            datetimeScript.Append(" if(r[6].length == 1) r[6] = '0' + r[6]; ");
256InBlock.gif            datetimeScript.Append(" if(r[7].length == 1) r[7] = '0' + r[7]; ");
257InBlock.gif            datetimeScript.Append(" document.getElementById(inputID).value = r[1] + '-' + r[3] + '-' + r[4] + ' ' + r[5] + ':' + r[6] + ':' + r[7]; ");
258InBlock.gif            datetimeScript.Append(" document.getElementById(spanID).innerText = ''; ");
259InBlock.gif
260InBlock.gif            datetimeScript.Append(" return true; ");
261InBlock.gif            datetimeScript.Append("} ");
262InBlock.gif            datetimeScript.Append("</script> ");
263InBlock.gif
264InBlock.gif            return datetimeScript.ToString();
265InBlock.gif
266ExpandedSubBlockEnd.gif        }

267InBlock.gif
268ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
269InBlock.gif        /// 获取验证日期的 javascript
270InBlock.gif        /// </summary>
271InBlock.gif        /// <param name="errMessage">验证错误时的提示信息</param>
272ExpandedSubBlockEnd.gif        /// <returns>验证日期的 javascript</returns>

273InBlock.gif        public string GetIsDateScript(string errMessage)
274ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
275InBlock.gif            StringBuilder dateScript = new StringBuilder();
276InBlock.gif
277InBlock.gif            dateScript.Append("<script language=javascript> ");
278InBlock.gif            dateScript.Append("function IsDate(inputID, spanID) ");
279InBlock.gif            dateScript.Append("{ ");
280InBlock.gif
281InBlock.gif            dateScript.Append(" var inputValue = document.getElementById(inputID).value; ");
282InBlock.gif
283InBlock.gif            dateScript.Append(" if(inputValue == '') ");
284InBlock.gif            dateScript.Append(" { ");
285InBlock.gif//            dateScript.Append("  document.getElementById(spanID).innerText = '请输入日期'; ");
286InBlock.gif            dateScript.Append("  alert('请输入日期!');");
287InBlock.gif            dateScript.Append("  return false; ");
288InBlock.gif            dateScript.Append(" } ");
289InBlock.gif
290InBlock.gif            dateScript.Append(" var reg = /^(\\d{1,4})(-|\\/|\\.)(\\d{1,2})\\2(\\d{1,2})$/; ");
291InBlock.gif            dateScript.Append(" var r = inputValue.match(reg); ");
292InBlock.gif
293InBlock.gif            dateScript.Append(" if(r == null) ");
294InBlock.gif            dateScript.Append(" { ");
295InBlock.gif            dateScript.Append("  alert('" + errMessage + "'); ");
296InBlock.gif            dateScript.Append("  document.getElementById(inputID).focus(); ");
297InBlock.gif            dateScript.Append("  return false; ");
298InBlock.gif            dateScript.Append(" } ");
299InBlock.gif
300InBlock.gif            dateScript.Append(" var d = new Date(r[1], r[3]-1, r[4]) ");
301InBlock.gif
302InBlock.gif            dateScript.Append(" if(r[1] != d.getFullYear() || r[3] != d.getMonth() + 1 || r[4] != d.getDate()) ");
303InBlock.gif            dateScript.Append(" { ");
304InBlock.gif            dateScript.Append("  alert('" + errMessage + "'); ");
305InBlock.gif            dateScript.Append("  document.getElementById(inputID).focus(); ");
306InBlock.gif            dateScript.Append("  return false; ");
307InBlock.gif            dateScript.Append(" } ");
308InBlock.gif
309InBlock.gif
310InBlock.gif            dateScript.Append(" if(r[3].length == 1) r[3] = '0' + r[3]; ");
311InBlock.gif            dateScript.Append(" if(r[4].length == 1) r[4 ]= '0' + r[4]; ");
312InBlock.gif
313InBlock.gif
314InBlock.gif            dateScript.Append(" var newValue = r[1] + '-' + r[3] + '-' + r[4]; ");
315InBlock.gif            dateScript.Append(" document.getElementById(inputID).value = newValue; ");
316InBlock.gif            dateScript.Append("  document.getElementById(spanID).innerText = ''; ");
317InBlock.gif
318InBlock.gif            dateScript.Append(" return true; ");
319InBlock.gif            dateScript.Append("} ");
320InBlock.gif            dateScript.Append("</script>");
321InBlock.gif
322InBlock.gif            return dateScript.ToString();
323ExpandedSubBlockEnd.gif        }

324InBlock.gif
325InBlock.gif
326ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
327InBlock.gif        /// 获取验证时间的 javascript
328InBlock.gif        /// </summary>
329InBlock.gif        /// <param name="errMessage">验证错误时的提示信息</param>
330ExpandedSubBlockEnd.gif        /// <returns>验证时间的 javascript</returns>

331InBlock.gif        public string GetIsTimeScript(string errMessage)
332ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
333InBlock.gif            StringBuilder timeScript = new StringBuilder();
334InBlock.gif
335InBlock.gif            timeScript.Append("<script language=javascript> ");
336InBlock.gif            timeScript.Append("function IsTime(inputID, spanID) ");
337InBlock.gif            timeScript.Append("{ ");
338InBlock.gif            timeScript.Append(" var inputValue = document.getElementById(inputID).value; ");
339InBlock.gif
340InBlock.gif            timeScript.Append(" if(inputValue == '') ");
341InBlock.gif            timeScript.Append(" { ");
342InBlock.gif//            timeScript.Append("  document.getElementById(spanID).innerText = '请输入时间'; ");
343InBlock.gif            timeScript.Append("   alert('请输入时间');");
344InBlock.gif            timeScript.Append("  return false; ");
345InBlock.gif            timeScript.Append(" } ");
346InBlock.gif
347InBlock.gif
348InBlock.gif            timeScript.Append(" var reg = /^(\\d{1,2}):(\\d{1,2}):(\\d{1,2})$/; ");
349InBlock.gif            timeScript.Append(" var r = inputValue.match(reg); ");
350InBlock.gif
351InBlock.gif            timeScript.Append(" if(r == null) ");
352InBlock.gif            timeScript.Append(" { ");
353InBlock.gif            timeScript.Append("  alert('" + errMessage + "'); ");
354InBlock.gif            timeScript.Append("  document.getElementById(inputID).focus(); ");
355InBlock.gif
356InBlock.gif            timeScript.Append("  return false; ");
357InBlock.gif            timeScript.Append(" } ");
358InBlock.gif
359InBlock.gif            timeScript.Append(" if(r[1] > 24 || r[2] > 60 || r[3] > 60) ");
360InBlock.gif            timeScript.Append(" { ");
361InBlock.gif            timeScript.Append("  alert('" + errMessage + "'); ");
362InBlock.gif            timeScript.Append("  document.getElementById(inputID).focus(); ");
363InBlock.gif            timeScript.Append("  return false; ");
364InBlock.gif            timeScript.Append(" } ");
365InBlock.gif
366InBlock.gif            timeScript.Append(" if(r[1].length == 1) r[1] = '0' + r[1]; ");
367InBlock.gif            timeScript.Append(" if(r[2].length == 1) r[2] = '0' + r[2]; ");
368InBlock.gif            timeScript.Append(" if(r[3].length == 1) r[3] = '0' + r[3]; ");
369InBlock.gif
370InBlock.gif            timeScript.Append(" document.getElementById(inputID).value = r[1] + ':' + r[2] + ':' + r[3]; ");
371InBlock.gif            timeScript.Append("  document.getElementById(spanID).innerText = ''; ");
372InBlock.gif
373InBlock.gif            timeScript.Append(" return true; ");
374InBlock.gif            timeScript.Append("} ");
375InBlock.gif            timeScript.Append("</script> ");
376InBlock.gif
377InBlock.gif            return timeScript.ToString();
378ExpandedSubBlockEnd.gif        }

379InBlock.gif
380InBlock.gif
381ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
382InBlock.gif        /// 获取验证时间的 javascript
383InBlock.gif        /// </summary>
384InBlock.gif        /// <param name="errMessage">验证错误时的提示信息</param>
385ExpandedSubBlockEnd.gif        /// <returns>验证时间的 javascript</returns>

386InBlock.gif        public string GetIsNumScript(string errMessage)
387ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
388InBlock.gif            StringBuilder numScript = new StringBuilder();
389InBlock.gif
390InBlock.gif            numScript.Append("<script language=javascript> ");
391InBlock.gif            numScript.Append("function IsNum(inputID, spanID) ");
392InBlock.gif            numScript.Append("{ ");
393InBlock.gif            numScript.Append("    var inputValue = document.getElementById(inputID).value; ");
394InBlock.gif            numScript.Append("    if(isNaN(inputValue)) ");
395InBlock.gif            numScript.Append("    { ");
396InBlock.gif            numScript.Append("        alert('请输入数字!'); ");
397InBlock.gif            numScript.Append("      document.getElementById(inputID).focus(); ");
398InBlock.gif            numScript.Append("        return false; ");
399InBlock.gif            numScript.Append("    } ");
400InBlock.gif
401InBlock.gif
402InBlock.gif            numScript.Append(" return true; ");
403InBlock.gif            numScript.Append("} ");
404InBlock.gif            numScript.Append("</script> ");
405InBlock.gif
406InBlock.gif            return numScript.ToString();
407ExpandedSubBlockEnd.gif        }

408InBlock.gif
409ExpandedSubBlockEnd.gif        #endregion

410ExpandedSubBlockEnd.gif    }

411ExpandedBlockEnd.gif}

本文转自浪子博客园博客,原文链接:http://www.cnblogs.com/walkingboy/archive/2005/09/19/239654.html,如需转载请自行联系原作者
目录
相关文章
|
4月前
ant-design 设置Form.Item中的input框的值的方法
ant-design 设置Form.Item中的input框的值的方法
222 0
|
11月前
【element-ui用法】el-radio-group默认选择和数据回显问题的解决方案
【element-ui用法】el-radio-group默认选择和数据回显问题的解决方案
518 0
jq获取select控件的selected属性值
jq获取select控件的selected属性值
|
开发者
134. SAP UI5 Simple Form 属性 columnsL,columnsM,columnsXL 的属性深入剖析
134. SAP UI5 Simple Form 属性 columnsL,columnsM,columnsXL 的属性深入剖析
|
数据安全/隐私保护
一步一步学Edit Control控件的用法
Edit Control控件最常见的用法,一般有有以下几种: 1、  显示默认的字符串; 2、  接受用户输入的字符串。 3、  作为密码框接受用户输入的字符串。
1107 0
PyQt5 技术篇-设置输入框的placeholder方法,Qt Designer设置Line Edit、Text Edit编辑框的placeholder
PyQt5 技术篇-设置输入框的placeholder方法,Qt Designer设置Line Edit、Text Edit编辑框的placeholder
670 0
PyQt5 技术篇-设置输入框的placeholder方法,Qt Designer设置Line Edit、Text Edit编辑框的placeholder