/// <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输入的更多相关文章

  1. js用正则表达式控制价格输入

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  2. 正则表达式控制Input输入内容 ,js正则验证方法大全

    https://blog.csdn.net/xushichang/article/details/4041507 //输入姓名的正则校验 e.currentTarget.value = e.curre ...

  3. 限制TextBox输入,只能输入Double类型数字

    public class TextBoxDouble : TextBox { public TextBoxDouble() { KeyDown += TextBoxDouble_KeyDown; Te ...

  4. 限制TextBox输入,只能输入整数

    public class TextBoxInt : TextBox { public TextBoxInt() { KeyDown += TextBoxInt_KeyDown; TextChanged ...

  5. WPF 限制Textbox输入的内容

    限制文本框TextBox的输入内容,在很多场景都有应用.举个例子,现在文本框中,只能输入0.1.2.3.4.5.6.7.8.9.“|”这11个字符. 限制输入0-9很容易实现,关键是这个“|”符号.它 ...

  6. C#winform控制textbox输入只能为数字

    添加keyPress事件,控制键盘输入只能是自然数: /// <summary> /// 控制键盘输入只能是自然数 /// </summary> /// <param n ...

  7. WPF 之 实现TextBox输入文字后自动弹出数据(类似百度的输入框)

    1.添加一个数据实体类 AutoCompleteEntry,如下: using System; using System.Collections.Generic; using System.Linq; ...

  8. 使用css使textbox输入内容自动变大写

    <style type="text/css"> input[type="text"] { text-transform:uppercase; } & ...

  9. JavaScript实现在textbox输入时自动去数据库匹配并找出类似值列出,选择后记得将值填入本textbox及下一个textbox

    1. <script src='<%= Application["rootURL"] %>JS/jquery-1.4.1.min.js' type="t ...

随机推荐

  1. centos 7 epel地址

    wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm

  2. 切记ajax中要带上AntiForgeryToken防止CSRF攻击

    在程序项目中经常看到ajax post数据到服务器没有加上防伪标记,导致CSRF被攻击,下面小编通过本篇文章给大家介绍ajax中要带上AntiForgeryToken防止CSRF攻击,感兴趣的朋友一起 ...

  3. Oracle数据库中如何选择合适的索引类型 .

    索引就好象一本字典的目录.凭借字典的目录,我们可以非常迅速的找到我们所需要的条目.数据库也是如此.凭借Oracle数据库的索引,相关语句可以迅速的定位记录的位置,而不必去定位整个表. 虽然说,在表中是 ...

  4. PHP实现简单爬虫

    <?php /**  * 爬虫程序 -- 原型  *  * 从给定的url获取html内容  *  * @param string $url  * @return string  */ func ...

  5. MySQL数据库主从同步安装与配置总结

    MySQL的主从同步是一个很成熟的架构,优点为: ①在从服务器可以执行查询工作(即我们常说的读功能),降低主服务器压力: ②在从主服务器进行备份,避免备份期间影响主服务器服务: ③当主服务器出现问题时 ...

  6. flask 后台表单验证模块

    我不想直接用flask的wtf模块,太大,功能太多,用不了.但后台也不能不做验证吧,我比较懒,不想一行一行写代码验证,所以就写了一个验证模块,对于小项目也够用了 # encoding=utf-8 # ...

  7. MySQL load data infile

    语法: load data [low_priority] [local] infile ‘file_path' [replace] [ignore] into table table_name [(c ...

  8. 混合使用Azure LB和ILB访问相同web服务(2)

    那么现在开始,我们配置下两台WEB服务器的Internal Load  balancer: 打开Powershell,检查当前两台虚拟机的端点配置: Get-AzureVM  -ServiceName ...

  9. C#线程间通讯

    using System;using System.Text;using System.Windows.Forms;using System.Threading; namespace 线程间通讯{   ...

  10. Zepto,Zepto API 中文版,Zepto 中文手册,Zepto API,Zepto API 中文版,Zepto 中文手册,Zepto API 1.0, Zepto API 1.0 中文版,Zepto 1.0 中文手册,Zepto 1.0 API-translate by yaotaiyang

    Zepto,Zepto API 中文版,Zepto 中文手册,Zepto API,Zepto API 中文版,Zepto 中文手册,Zepto API 1.0, Zepto API 1.0 中文版,Z ...