带验证功能的的TextBox

 TextBox是用的比较多的控件,有时候我们希望用户的输入内容和符合我们的输入要求,按这个想法在原有控件的基础上,做了个能控制输入的TextBox,有兴趣的可以用用.原码如下.

  1 None.gif using  System;
  2 None.gif using  System.Collections.Generic;
  3 None.gif using  System.Text;
  4 None.gif using  System.ComponentModel;
  5 None.gif using  System.Web.UI;
  6 None.gif using  System.Web.UI.WebControls;
  7 None.gif using  System.Drawing;
  8 None.gif namespace  Jsl.Web.UI.WebControls
  9 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 10ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 11InBlock.gif    /// 封装的TextBox,可以设置为文本框,整数文本框,浮点数文本框
 12InBlock.gif    /// 
 13ExpandedSubBlockEnd.gif    /// </summary>

 14InBlock.gif    public class JslTextBox : TextBox 
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 16InBlock.gif
 17InBlock.gif
 18ContractedSubBlock.gifExpandedSubBlockStart.gif        IWebControl 成员#region IWebControl 成员
 19ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 20InBlock.gif        /// 是否以文本方式显示
 21InBlock.gif
 22InBlock.gif        /// </summary>
 23ExpandedSubBlockEnd.gif        /// 

 24InBlock.gif        [Bindable(true)]
 25InBlock.gif        [Category("JSLProperty")]
 26InBlock.gif        [DefaultValue(false)]
 27InBlock.gif        [Localizable(true)]
 28InBlock.gif        [DescriptionAttribute("是否以文本方式显示")]
 29InBlock.gif        public bool ShowAsLabel
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 31InBlock.gif            get
 32ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 33InBlock.gif                if (ViewState["ShowAsLabel"!= null)
 34InBlock.gif                    return (bool)ViewState["ShowAsLabel"];
 35InBlock.gif                return false;
 36ExpandedSubBlockEnd.gif            }

 37InBlock.gif            set
 38ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 39InBlock.gif                ViewState["ShowAsLabel"= value;
 40ExpandedSubBlockEnd.gif            }

 41ExpandedSubBlockEnd.gif        }

 42ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 43InBlock.gif        /// 是否开启输入控制
 44InBlock.gif
 45ExpandedSubBlockEnd.gif        /// </summary>

 46InBlock.gif        [Bindable(true)]
 47InBlock.gif        [Category("JSLProperty")]
 48InBlock.gif        [DefaultValue(false)]
 49InBlock.gif        [Localizable(true)]
 50InBlock.gif        [DescriptionAttribute("是否开启输入控制.")]
 51InBlock.gif        public bool Pree
 52ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 53InBlock.gif            get
 54ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 55InBlock.gif
 56InBlock.gif                object obj1 = this.ViewState["#pree#"];
 57InBlock.gif                if (obj1 != null)
 58ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 59InBlock.gif                    return (bool)obj1;
 60ExpandedSubBlockEnd.gif                }

 61InBlock.gif                return false;
 62ExpandedSubBlockEnd.gif            }

 63InBlock.gif            set
 64ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 65InBlock.gif                ViewState["#pree#"= value;
 66ExpandedSubBlockEnd.gif            }

 67ExpandedSubBlockEnd.gif        }

 68ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 69InBlock.gif        /// 控制类型
 70ExpandedSubBlockEnd.gif        /// </summary>

 71InBlock.gif        [Bindable(true)]
 72InBlock.gif        [Category("JSLProperty")]
 73InBlock.gif        [DefaultValue(PreeClass.只能输入英文和中文)]
 74InBlock.gif        [Localizable(true)]
 75InBlock.gif        [DescriptionAttribute("控制类型.")]
 76InBlock.gif        public PreeClass PreeType
 77ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 78InBlock.gif            get
 79ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 80InBlock.gif
 81InBlock.gif                object obj1 = this.ViewState["#preetype#"];
 82InBlock.gif                if (obj1 != null)
 83ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 84InBlock.gif                    return (PreeClass)obj1;
 85ExpandedSubBlockEnd.gif                }

 86InBlock.gif                return PreeClass.只能输入中文;
 87ExpandedSubBlockEnd.gif            }

 88InBlock.gif            set
 89ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 90InBlock.gif                ViewState["#preetype#"= value;
 91ExpandedSubBlockEnd.gif            }

 92ExpandedSubBlockEnd.gif        }

 93InBlock.gif       
 94ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 95InBlock.gif        /// 载入
 96InBlock.gif        /// </summary>
 97ExpandedSubBlockEnd.gif        /// <param name="e"></param>

 98InBlock.gif        protected override void OnLoad(EventArgs e)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
100InBlock.gif            
101InBlock.gif            base.OnLoad(e);
102InBlock.gif            if (Pree)
103ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
104InBlock.gif                string script = "";
105InBlock.gif                switch(PreeType.ToString())
106ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
107InBlock.gif                    case "只能输入英文和中文":
108InBlock.gif                     script = @"value=value.replace(/[ -/:-@\[-`0-9{-~]/, '')";
109InBlock.gif                     break;
110InBlock.gif                 case "只能输入中文":
111InBlock.gif                        script = "value=value.replace(/[0-9a-zA-Z_!-|]/g, '')";
112InBlock.gif                        break;
113InBlock.gif                    case "禁止输入中文":
114InBlock.gif                        script = "value=value.replace(/^[\u4E00-\u9FA5]+$/, '')";
115InBlock.gif                        break;
116InBlock.gif                    case "禁止输入数字":
117InBlock.gif                        script = "value=value.replace(/[0-9]/g, '')";
118InBlock.gif                        break;
119InBlock.gif                    case "只能输入数字":
120InBlock.gif                        script = "value=value.replace(/[^0-9_]/ig, '')";
121InBlock.gif                        break;
122InBlock.gif                    case "禁止输入英文":
123InBlock.gif                        script = "value=value.replace(/[A-Za-z]/g, '')";
124InBlock.gif                        break;
125InBlock.gif                    case "只能输入英文":
126InBlock.gif                        script = "value=value.replace(/[^a-zA-Z_]/ , '')";
127InBlock.gif                        break;
128InBlock.gif                   
129ExpandedSubBlockEnd.gif                }

130InBlock.gif
131InBlock.gif                this.Attributes.Add("onkeyup", script);
132ExpandedSubBlockEnd.gif            }

133ExpandedSubBlockEnd.gif        }

134ExpandedSubBlockEnd.gif        #endregion

135InBlock.gif
136InBlock.gif     
137InBlock.gif
138InBlock.gif       
139InBlock.gif       
140ExpandedSubBlockEnd.gif    }

141InBlock.gif    
142ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
143InBlock.gif    /// 文本框对象
144InBlock.gif
145ExpandedSubBlockEnd.gif    /// </summary>

146InBlock.gif    public enum PreeClass
147ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
148ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
149InBlock.gif        /// 只能输入英文和中文
150InBlock.gif
151ExpandedSubBlockEnd.gif        /// </summary>

152InBlock.gif        只能输入英文和中文 = 0,
153ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
154InBlock.gif        /// 只能输入中文
155ExpandedSubBlockEnd.gif        /// </summary>

156InBlock.gif        只能输入中文 = 1,
157ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
158InBlock.gif        /// 禁止输入中文
159ExpandedSubBlockEnd.gif        /// </summary>

160InBlock.gif        禁止输入中文 = 2,
161ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
162InBlock.gif        /// 禁止输入数字
163ExpandedSubBlockEnd.gif        /// </summary>

164InBlock.gif        禁止输入数字 = 3,
165ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
166InBlock.gif        /// 只能输入数字
167ExpandedSubBlockEnd.gif        /// </summary>

168InBlock.gif        只能输入数字 = 4,
169ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
170InBlock.gif        /// 禁止输入英文
171ExpandedSubBlockEnd.gif        /// </summary>

172InBlock.gif        禁止输入英文 = 5,
173ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
174InBlock.gif        /// 只能输入英文
175ExpandedSubBlockEnd.gif        /// </summary>

176InBlock.gif        只能输入英文 = 6
177ExpandedSubBlockEnd.gif    }

178ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/DarkAngel/archive/2006/09/23/512649.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值