110,577
社区成员
发帖
与我相关
我的任务
分享
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.'))
{
if (!PrmedSysKey.Contains((int)e.KeyChar))
e.KeyChar = '\0';
}
}
/// <summary>
/// 特殊键
/// </summary>
private List<int> PrmedSysKey
{
get
{
int[] iOthChar = new int[] { 3, 22,8 };//ctrl+c,ctrl+v,后退
List<int> list = new List<int>();
list.Add(3);//ctrl+c,
list.Add(22);//ctrl+v
list.Add(8);//后退
list.AddRange(iOthChar);
return list;
}
}
private void textBox1_Leave(object sender, EventArgs e)
{
if (!Regex.IsMatch(textBox1.Text,this.RegFixedNum))
{
MessageBox.Show("Wrong format input!");
textBox1.Focus();
}
}
/// <summary>
/// 正则
/// </summary>
private string RegFixedNum
{
get
{
return @"^(?:0|[^0]\d{0,7}|(?:0|[^0]\d{0,7})\.\d{1,2})$";
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
Regex re = new Regex(@"^(?:0|[^0]\d{0,7}|(?:0|[^0]\d{0,8})\.\d{1,2})$");
string[] slist = {"0.33","12345678.33","12345678","123456789","0","1","01","01.11","01.333"};
foreach (string s in slist)
{
Console.WriteLine("{0} matches? {1}", s, re.IsMatch(s));
}
}
}
}
^\d{8}(.\d{2})?$
^\d{1,8}(\.\d{1,2})$