wpf中的正则表达式验证 [英] regex validation in wpf

查看:565
本文介绍了wpf中的正则表达式验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Que 1)我希望在输入值后只有小数点,之后会有两个小数点。

除了。(点)或字母之外没有其他特殊字符



我的代码是

< pre lang =c#> private static bool IsTextNumeric( string str)
{
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex( [^ 0-9 ^。{1} ^ 0- 9] $);
return reg.IsMatch(str);
}
private void NumericOnly(System。 Object sender,System.Windows.Input.TextCompositionEventArgs e)
{
e.Handled = IsTextNumeric(e.Text);
}

private void txtbx_Capcity_PreviewTextInput( object sender,TextCompositionEventArgs e)
{
NumericOnly(txtbx_Capcity,e);
}







Que 2)我还想要如果可能的话,使用正则表达式限制Min和Max之间的文本框中的值。



先谢谢你的帮助

解决方案

);
return reg.IsMatch(str);
}
private void NumericOnly(System。 Object sender,System.Windows .Input.TextCompositionEventArgs e)
{
e.Handled = IsTextNumeric(e.Text);
}

private void txtbx_Capcity_PreviewTextInput( object sender,TextCompositionEventArgs e)
{
NumericOnly(txtbx_Capcity,e);
}







Que 2)另外,如果po,我想限制Min和Max之间的文本框中的值sgeible使用正则表达式。



先谢谢你的帮助


你的正则表达式不起作用。 是一个rexeg中的匹配任何字符字符,因此对于文字小数点,你想要\。

如果你在数字之后,指向两个数字然后尝试:

 ^ \d + \.\d { 2 } 




 System.Text.RegularExpressions.Regex reg =  new 系统。 Text.RegularExpressions.Regex( @  ^ \d + \。\\\ {2} 


Que 1)i would like to have only decimal point after value entered and two decimal points later.
No other special character other than .(dot) or alphabet is allowed

my code is

private static bool IsTextNumeric(string str)
{
  System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("[^0-9^.{1}^0-9]$");
            return reg.IsMatch(str);
}
 private void NumericOnly(System.Object sender, System.Windows.Input.TextCompositionEventArgs e)
{
   e.Handled = IsTextNumeric(e.Text);
}

private void txtbx_Capcity_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
   NumericOnly(txtbx_Capcity, e);
}




Que 2)Also I would like restrict the value in text box between Min and Max if possible using regex.

Thanks in Advance for Ur help

解决方案

"); return reg.IsMatch(str); } private void NumericOnly(System.Object sender, System.Windows.Input.TextCompositionEventArgs e) { e.Handled = IsTextNumeric(e.Text); } private void txtbx_Capcity_PreviewTextInput(object sender, TextCompositionEventArgs e) { NumericOnly(txtbx_Capcity, e); }




Que 2)Also I would like restrict the value in text box between Min and Max if possible using regex.

Thanks in Advance for Ur help


Your regex won''t work. "." is a "match any character" character in a rexeg, so for a literal decimal point, you want "\."
If you are after numbers, point, two numbers then try:

^\d+\.\d{2}



System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"^\d+\.\d{2}


这篇关于wpf中的正则表达式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆