如何验证文本框,以便它只接受正数 [英] How to validate the textbox ,so that it should accept only the positive numbers

查看:58
本文介绍了如何验证文本框,以便它只接受正数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个文本框,它应该只接受正整数,它不应该接受任何其他特殊字符。

请做必要的。

解决方案

不要验证它,过滤掉除了数字之外的每个字符。更确切地说,验证较晚,但首先在用户键入值时过滤掉我们不需要的字符。您没有标记您正在使用的UI库或应用程序类型,但在所有情况下都可以这样做。通常,这是事件的处理 KeyPress



除此之外,验证值。首先,如果你没有输入数字或太多,它不会被解析为任何整数类型,所以首先尝试 System.UInt64.TryParse(yourTextBoxText)(或其他适当的无符号整数类型)。解析成功后,您还可以验证所需的值范围。



-SA


< blockquote>你可以通过正则表达式来实现它可能是windows或web。



i会给你链接。只需通过





https://gkskonline.wordpress.com/2013/05/20/email-validation-using-regex/ [ ^ ]


如果你合作WinForms可以使用 MaskedTextBox 控件,掩码属性设置为000000,用于0到999999之间的数字

I want to display a textbox ,which should accept only positive integer,it should not accept any other special charecters.
please do the needful.

解决方案

Don''t validate it, filter out every characters but digits. More exactly, validate late, but first filter our unwanted characters as the user types the value in. You did not tag the UI library or application type you are using, but in all cases this is possible. Typically, this is handling of the event KeyPress.

On top of it, validate the value. First of all, if you type no digits or too many, it won''t be parsed as any integer type, so first try System.UInt64.TryParse(yourTextBoxText) (or other appropriate unsigned integer type). When parsing is successful, you can additionally validate for required range of the value.

—SA


you can do it by regular expression it may be windows or web.

i will give you the link. just go through


https://gkskonline.wordpress.com/2013/05/20/email-validation-using-regex/[^]


if you work with WinForms it''s possible to use MaskedTextBox control with Mask property set into 000000 for numbers from 0 to 999999


这篇关于如何验证文本框,以便它只接受正数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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