正则式限制文本只能输入小数

Lkmwyeb 2009-01-13 05:32:04
是输入时就限制小数点后面只能输入2位 只能输入一个点
输入了小数点能输入11位
没输入小数点只能数8位

试了很多 都是不很好 希望那位大哥能帮帮小弟啊

注: 是Winform Text
...全文
220 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lkmwyeb 2009-01-16
  • 打赏
  • 举报
回复
我也想到一个
不过就是输入了.后面还是不能控制

System.Windows.Forms.TextBox txt = (System.Windows.Forms.TextBox)sender;
try
{
Convert.ToDecimal(txt.Text +
e.KeyChar.ToString().Replace("\b", "").Replace("", "").Replace("", "").Replace("", "")
+ "0");
if (e.KeyChar == '\b' || e.KeyChar == 22 || e.KeyChar == 3 || e.KeyChar == 24)
{
e.Handled = false;
}
else if (txt.Text.Length - (txt.Text + e.KeyChar.ToString().Replace("\b", "")).IndexOf(".") > 2 &&
(e.KeyChar.ToString() + txt.Text).IndexOf(".") > 8)
{
e.Handled = true;
}
}
catch
{
e.Handled = true;
}
CutBug 2009-01-13
  • 打赏
  • 举报
回复
上面的函数改一下
CutBug 2009-01-13
  • 打赏
  • 举报
回复
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;
}
}
CutBug 2009-01-13
  • 打赏
  • 举报
回复
我觉得可以先限定只能输入.和数字,格式leave的时候再做判断
 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})$";

}
}
Lkmwyeb 2009-01-13
  • 打赏
  • 举报
回复
大家给的好像都是返回ture/false
我在
KeyUp事件里面
this.txt.Text = System.Text.RegularExpressions.
Regex.Replace(this.txt.Text, @"^(?:0|[^0]\d{0,7}|(?:0|[^0]\d{0,8})\.\d{1,2})$", "");
什么都输入不了 但输入快点就什么都能输入了
KeyPress事件里面
Regex re = new Regex(@"^(?:0|[^0]\d{0,7}|(?:0|[^0]\d{0,8})\.\d{1,2})$");
if (re.IsMatch(txt.Text)) e.Handled = false; //如果吧txt.text改成e.keychar 就和KeyUp一样
else e.Handled = true;//方正现在什么都不能输入
TextChanged事件里面
string str = System.Text.RegularExpressions.
Regex.Replace(this.txtValue2.Text, @"^(?:0|[^0]\d{0,7}|(?:0|[^0]\d{0,8})\.\d{1,2})$", "").ToString();
//就算错了它返回的还是错的 只能放回ture/false
this.txtValue2.Text = str;

我现在只想这样
输入错的只是限制不能输入
CutBug 2009-01-13
  • 打赏
  • 举报
回复
Regex re = new Regex(@"^(?:0|[^0]\d{0,7}|(?:0|[^0]\d{0,7})\.\d{1,2})$");
CutBug 2009-01-13
  • 打赏
  • 举报
回复
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));
}

}

}
}


输出:
0.33 matches? True
12345678.33 matches? True
12345678 matches? True
123456789 matches? False
0 matches? True
1 matches? True
01 matches? False
01.11 matches? False
01.333 matches? False
GTX280 2009-01-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 gzdiablo 的回复:]
C# code^\d{1,8}(\.\d{1,2})$
[/Quote]
up
wb186 2009-01-13
  • 打赏
  • 举报
回复
正则表达式 噢噢噢噢
fuyuxin19801120 2009-01-13
  • 打赏
  • 举报
回复
正规表达式 整数补零 小数截取
wackyboy 2009-01-13
  • 打赏
  • 举报
回复

^\d{8}(.\d{2})?$
gzdiablo 2009-01-13
  • 打赏
  • 举报
回复
^\d{1,8}(\.\d{1,2})$
owenliangbin 2009-01-13
  • 打赏
  • 举报
回复
结合2楼使用掩码
万小萌 2009-01-13
  • 打赏
  • 举报
回复
每次点击判断是否符合格式,然后决定是否显示输入不就ok了?

110,577

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧