窗口形式文本框验证 [英] Window form textbox validation

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

问题描述

我正在尝试验证文本框以接受具有正则表达式的特定格式的用户输入。但不幸的是,我无法获得理想的结果。



我需要用户输入前3位数作为固定数字405,然后是1950年当前年份,然后是六位数唯一标识符,我可以得到一些帮助吗?



任何建议和帮助表示赞赏。



谢谢。

解决方案

不要使用正则表达式,或者不要单独使用正则表达式。

他们不是擅长数字范围 - 它们是一个文本处理系统。

因此使用正则表达式来分解输入:

 ^( ?<前缀GT; \d {3})(小于年> \d {4})(小于?流体> \d {6})

< ; / uid>< / year>< /前缀>

然后使用三个命名组来验证实际内容。它更灵活,更容易理解(并且明年也可以工作......)


你的用户界面,如果你按照自己的意愿实现它,将真的是用户敌对的。



让用户更简单。如果405真的总是在那里(我怀疑那个),只需将它写在某个标签上,不要强迫用户输入它。至于范围内的年份,使用控件 System.Windows.Forms.NumericUpDown 并设置所需的值范围:

https://msdn.microsoft.com/en- us / library / system.windows.forms.numericupdown%28v = vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system .windows.forms.numericupdown.minimum(v = vs.110).aspx [ ^ ],

https://msd n.microsoft.com/en-us/library/system.windows.forms.numericupdown.maximum(v=vs.110).aspx [ ^ ]。



如果你使用WPF,创建一个类似的控件来找出类似的。例如,请参阅:从头开始创建NumericUpDown控件 [ ^ ]。br />


另一种选择是某种更专业的日期/时间选择器。



-SA

I am trying to validate a textbox to accept user input in certain format with regular expressions. But unfortunately I was not able to get desired results.

I need user input as first 3 digits as fixed number 405 followed by year ranging 1950-current year and followed by six digit unique identifier, can I please get some help with this?

Any suggestions and help is appreciated.

Thanks.

解决方案

Don't use a regex, or don;t use a regex alone.
They aren't good at "numeric ranges" - they are a text processing system.
So use a regex to break up the input:

^(?<prefix>\d{3})(?<year>\d{4})(?<uid>\d{6})


</uid></year></prefix>

And then use the three named groups to verify the actual content. It's more flexible, and a lot easier to understand (and make work next year as well...)


Your UI, if you implement it as you want, will really be user-hostile.

Make it simpler for the user. If "405" is really always there (I doubt that though), just write it in some label, don't force the user to enter it. As to the year in the range, use the control System.Windows.Forms.NumericUpDown and set the range of values you want:
https://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.minimum(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.numericupdown.maximum(v=vs.110).aspx[^].

If you use WPF, create a similar control of find out a similar one. See, for example: Creating a NumericUpDown control from scratch[^].

Another alternative is some kind of more specialized date/time picker.

—SA


这篇关于窗口形式文本框验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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