10月24

c# winform中用正则实现只能输入英文和数字

| |
08:36编程杂谈  From: 本站原创
首先要引入using System.Text.RegularExpressions;
string pattern = @"^[a-zA-Z0-9]+$";//正则式子
string param1 = null;
Match m = Regex.Match(this.textBox1.Text, pattern);   // 匹配正则表达式,把this.textBox1.Text跟pattern正则对比

if (!m.Success)   // 判断输入的是不是英文和数字,不是进入
{
    param1 = this.textBox1.Text;//将现在textBox的值保存下来
    // 将光标定位到文本框的最后
    this.textBox1.SelectionStart = this.textBox1.Text.Length;
}

else   //输入的是英文和数字
{
    param1 = this.textBox1.Text;   // 将现在textBox的值保存下来
}


来源:Heck's Blog
地址:https://www.heckjj.com/regex-to-achieve-can-only-eng-and-num/
转载时须以链接形式注明作者和原始出处及本声明,否则将追究法律责任,谢谢配合!
Tags: , ,
阅读(2219) | 评论(0) | 引用(0)