如何限制用户在C#文本框中的文本框长度末尾输入小数点 [英] How to restrict user from entering decimal point at the end of textbox length in the textbox in C#

查看:109
本文介绍了如何限制用户在C#文本框中的文本框长度末尾输入小数点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的texbox只接受数字和小数值。同样,使用以下正则表达式接受小数点前最多两位数和小数点后一位数。



正则表达式reg =新正则表达式([^ [0 -9。] +);



正则表达式re =新正则表达式(^ \\d {1,2}([。] \\ d {1})$);



我添加了一个条件,只接受一个小数点:



if(e.Text ==。&& txtPlannedHours.Text.IndexOf('。')> -1)

{

e。 Handled = true;

}
在previewTextInput事件中






有效值用户可以输入的是:

1111,1.5,12.5等..





用户的值无效不允许进入的是:

111.或234.等..





那么怎么样我可以避免用户输入最后一个小数点,即输入3位数后?(在文本框长度的第4个位置)



文本框的最大长度在wpf中给出为4(xaml)

Hi,

Am having a texbox which accepts only numeric and decimal values. As well to accepts max of two digits before the decimal and one digit after the decimal point using the following regex.

Regex reg = new Regex("[^[0-9.]+");

Regex re = new Regex("^\\d{1,2}([.]\\d{1})$");

And i have added an condition to accept only one decimal point using :

if (e.Text == "." && txtPlannedHours.Text.IndexOf('.') > -1)
{
e.Handled = true;
}
in the previewTextInput event.


The valid values which the user can enter are:
1111,1.5,12.5 etc..


Invalid value which user should not be allowed to enter is :
111. or 234. etc..


So how can i avoid user from entering a decimal point in the last, i.e after entering 3 digits?(at the 4th position of the textbox length)

The max length of textbox is given as 4 in wpf(xaml)

推荐答案

);



我添加了一个使用以下条件仅接受一个小数点:



if(e.Text ==。&& txtPlannedHours.Text.IndexOf('。')> -1 )

{

e.Handled = true;

}
在previewTextInput事件中






有效用户可以输入的值是:

1111,1.5,12.5等..





无效值不允许进入的用户是:

111.或234.等..





那么如何避免用户输入最后一个小数点,即输入3位数后?(在文本框长度的第4个位置)



最大长度文本框的格式为4,格式为wpf(xaml)
");

And i have added an condition to accept only one decimal point using :

if (e.Text == "." && txtPlannedHours.Text.IndexOf('.') > -1)
{
e.Handled = true;
}
in the previewTextInput event.


The valid values which the user can enter are:
1111,1.5,12.5 etc..


Invalid value which user should not be allowed to enter is :
111. or 234. etc..


So how can i avoid user from entering a decimal point in the last, i.e after entering 3 digits?(at the 4th position of the textbox length)

The max length of textbox is given as 4 in wpf(xaml)


在Javascript函数中编写以下代码



WRite the following code in Javascript function

if (s.indexOf('.') >= 0) {
    return false;
}







并在文本框的ONkey按事件中调用该函数




and cal the function in ONkey press event for textbox


我认为你可以使用正则表达式(((\d {1,3})(,\\\ {3})*)|(\d +))(。\ d {2})?

它对我有用。它将在所需的位置和点上处理你的逗号。



谢谢
I think you can use the regular expression (((\d{1,3})(,\d{3})*)|(\d+))(.\d{2})?
Its works for me. It will handle your comma in required place and also the dot.

Thanks


这篇关于如何限制用户在C#文本框中的文本框长度末尾输入小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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