使用屏蔽文本框来验证输入
您好,我正在使用以下掩码字符串来确保用户输入有效的电话号码
(99)-00000000,
该电话号码对于澳大利亚的移动电话以及区域固定电话号码应该可以正常工作,
但是我遇到了一个问题,即用户可以在不输入所有电话号码的情况下离开 我知道9
代表 0 到 9 之间的可选数字,0 代表 0 到 9 之间的必需数字
那么,为什么如果我不输入所有最后八个强制数字,程序仍然显示类似
(03)-6474
(03) 的结果- 63799
(02)-1 38 390
Hi I am using the following mask string to ensure user enters a valid phone no
(99)-00000000
which should work fine for mobile as well as regional land line numbers in Australia
However I have encountered a problem that User can get away without entring all the digits
I understand that 9 represents an optional digit between 0 and 9 and 0 represents a required digit between 0 and 9
So how come if I dont enter all last eight mandatory digits the program still display results like
(03)-6474
(03)- 63799
(02)-1 38 390
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为它只是在您键入时从左到右填充空格。尽管前两位数字是可选的,但它会像您使用您键入的前两个数字一样输入它们。
如果您不喜欢这种行为,您可以轻松地子类化
MaskedTextBox
控件并自定义它处理键盘输入的方式。以下是几个示例控件,它们会导致数字按预期从右向左推入:http://www.codeproject.com/KB/edit/Cool_Phone_Number_Box.aspx
http://www.dotnetheaven.com/Uploadfile/mgold/MaskedCurrencyTextBox02252006005553AM/MaskedCurrencyTextBox.aspx
(我认为第二个例子可以更好地准确解释对基本控件所做的更改,而且它允许您在不下载示例项目的情况下查看大部分相关代码。)
Because it's simply filling in the blanks from left to right as you type. Even though the first two digits are optional, it fills them in like you've entered them with the first two numbers that you type.
If you dislike this behavior, you can easily subclass the
MaskedTextBox
control and customize how it handles keyboard input. Here are a couple of sample controls that cause the numbers to push in from right to left, as expected:http://www.codeproject.com/KB/edit/Cool_Phone_Number_Box.aspx
http://www.dotnetheaven.com/Uploadfile/mgold/MaskedCurrencyTextBox02252006005553AM/MaskedCurrencyTextBox.aspx
(I think the second example does a better job of explaining exactly what changes are being made to the base control, plus it allows you to see most of the relevant code without downloading the sample project.)