使用正则表达式限制TextBox输入
/// <summary>
/// 使用正则表达式限制输入
/// </summary>
public class TextBoxRegex : TextBox
{
// 正则表达式,只含有汉字、数字、字母、下划线不能以数字开头
private const string NamimgPattern = "^(?![0-9])[a-zA-Z0-9_\u4e00-\u9fa5]{1,30}$"; public TextBoxRegex()
{
TextChanged += TextBoxRegex_TextChanged;
} private void TextBoxRegex_TextChanged(object sender, TextChangedEventArgs e)
{
//屏蔽非法字符粘贴
var textBox = sender as TextBox;
if (textBox != null)
{
var change = new TextChange[e.Changes.Count];
e.Changes.CopyTo(change, ); int offset = change[].Offset;
if (change[].AddedLength > )
{
if (!Regex.IsMatch(textBox.Text, NamimgPattern, RegexOptions.IgnoreCase))
{
// 不符合正则表达式时移除内容,设置光标位置
textBox.Text = textBox.Text.Remove(offset, change[].AddedLength);
textBox.Select(offset, );
}
}
}
}
}
使用正则表达式限制TextBox输入的更多相关文章
- js用正则表达式控制价格输入
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- 正则表达式控制Input输入内容 ,js正则验证方法大全
https://blog.csdn.net/xushichang/article/details/4041507 //输入姓名的正则校验 e.currentTarget.value = e.curre ...
- 限制TextBox输入,只能输入Double类型数字
public class TextBoxDouble : TextBox { public TextBoxDouble() { KeyDown += TextBoxDouble_KeyDown; Te ...
- 限制TextBox输入,只能输入整数
public class TextBoxInt : TextBox { public TextBoxInt() { KeyDown += TextBoxInt_KeyDown; TextChanged ...
- WPF 限制Textbox输入的内容
限制文本框TextBox的输入内容,在很多场景都有应用.举个例子,现在文本框中,只能输入0.1.2.3.4.5.6.7.8.9.“|”这11个字符. 限制输入0-9很容易实现,关键是这个“|”符号.它 ...
- C#winform控制textbox输入只能为数字
添加keyPress事件,控制键盘输入只能是自然数: /// <summary> /// 控制键盘输入只能是自然数 /// </summary> /// <param n ...
- WPF 之 实现TextBox输入文字后自动弹出数据(类似百度的输入框)
1.添加一个数据实体类 AutoCompleteEntry,如下: using System; using System.Collections.Generic; using System.Linq; ...
- 使用css使textbox输入内容自动变大写
<style type="text/css"> input[type="text"] { text-transform:uppercase; } & ...
- JavaScript实现在textbox输入时自动去数据库匹配并找出类似值列出,选择后记得将值填入本textbox及下一个textbox
1. <script src='<%= Application["rootURL"] %>JS/jquery-1.4.1.min.js' type="t ...
随机推荐
- safari 在 iPad Portrait 模式默认设置980px宽度
最近在做网站兼容性时发现 safari 在 iPad Portrait 模式,默认为html.body标签设置了980px宽度,导致页面被纵向截断,解决方法为在页面head区插入以下代码即可完美解决. ...
- ie8下的透明 问题
团队里经常遇到,索性整理一起 是我们在前端开发中经常遇到的,在问题中经常遇到的两个问题是背景色透明和整体透明 先说下背景色透明,背景色透明,在现代浏览器中,可以用rgba颜色作为背景色. 简单介绍下r ...
- ecshop物料库存管理
1.创建物流库存表.sql语句: CREATE TABLE IF NOT EXISTS `emws_materials` (`id` mediumint(8) unsigned NOT NULL au ...
- flask 后台表单验证模块
我不想直接用flask的wtf模块,太大,功能太多,用不了.但后台也不能不做验证吧,我比较懒,不想一行一行写代码验证,所以就写了一个验证模块,对于小项目也够用了 # encoding=utf-8 # ...
- 字符串水题(hdoj1049)
Problem Description Password security is a tricky thing. Users prefer simple passwords that are easy ...
- python学习之day11
目录 SqlAlchemy 外键 SqlAlechemy SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是 ...
- Oracle EBS-SQL (PO-14):检查报价单与成本对比.sql
select distinct msi.segment1 项目 ,msi.de ...
- Storm Esper
https://github.com/hellojinjie https://github.com/tomdz/storm-esper https://github.com/jayway/awaiti ...
- Delphi调用安装驱动sys的单元
unit SysDriver; interface uses windows, winsvc; // jwawinsvc; Type TSysDriver = class(TObject) priva ...
- JavaEE Tutorials (27) - Java EE的并发工具
27.1并发基础427 27.1.1线程和进程42827.2并发工具的主要组件42827.3并发和事务42927.4并发和安全43027.5jobs并发示例430 27.5.1运行jobs示例4302 ...