C#输入表单验证 [英] C# Input form validation

查看:224
本文介绍了C#输入表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个表单供用户输入ID号,格式为'LLLNNNN'(L =字母,N =数字),例如JNH8383,除此之外的任何内容都应该被确定为无效。



此外,还需要检查任何以SOS和PAN开头的ID,当它们输入时需要设置错误信息。



提前感谢任何提示。

I'm trying to build a form for a user to input an ID number, in the format of 'LLLNNNN' (L=letter, N=number) so for example JNH8383 and anything other than that should be determined as invalid.

Also it needs to check for any ID's beginning with "SOS" and "PAN" when these are entered it needs to set off an error message.

Thanks in advance for any tips.

推荐答案

您可以使用各种选项来获取此类功能。



一个选项可能是使用蒙面文本框

MSDN - 蒙面文本框 [ ^ ]

但是你仍然需要编写一些验证来检查你的SOS和PAN​​值,这可以通过文本框离开事件中的简单字符串检查来完成。



另一个选项可能是在文本框的验证事件中将正则表达式与输入a进行比较nd触发相应的响应。



Windows窗体(MSDN)中的用户输入验证 [ ^ ]

正则表达式快速参考 - MSDN [ ^ ]
You have various options available to you for this sort of feature.

One option could be to use "Masked Text Box"
MSDN - Masked Text Box[^]
However you would still need to write some validation to check for your SOS and PAN values which could be done with a simple "In String" check in the textbox leave event.

Another option could be in the "Validating" event on the text box compare a regular expression to the input and trigger the appropriate response.

User Input Validation in Windows Forms (MSDN)[^]
Regular Expressions Quick Ref - MSDN[^]


您可能需要一个正则表达式来进行ID号验证。您可以使用创建一个[ ^ ]如果您不熟悉RegEx已经。



对于第二部分,一个简单的if语句和使用 substring() [ ^ ]方法就足够了。
You would probably need a regular expression for the ID number validation. You can create one with this[^] if you are not familiar with RegEx already.

For the second part, a simple if statement and the use of the substring()[^] method would suffice for this.


使用MaskedTextBox:

http://msdn.microsoft.com/en-us/library/vstudio/bb763018%28v=vs.100%29.aspx [ ^ ]



要验证控件使用Control.Validating事件:

http:// msdn .microsoft.com / zh-CN / library / system.windows.forms.control.validating%28v = vs.110%29.aspx [ ^ ]



要显示验证错误,请使用ErrorProvider组件:

http://msdn.microsoft.com/library/system.windows.forms.errorprovider%28v=vs.110%29.aspx [ ^ ]



要检查字符串是否以某些字母开头,请尝试使用此String.StartsWith方法。



我希望您找到这个答案有用:)
Use MaskedTextBox:
http://msdn.microsoft.com/en-us/library/vstudio/bb763018%28v=vs.100%29.aspx[^]

To validate control use Control.Validating event:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating%28v=vs.110%29.aspx[^]

To display validation error use ErrorProvider component:
http://msdn.microsoft.com/library/system.windows.forms.errorprovider%28v=vs.110%29.aspx[^]

To check if string starts with some letters try this String.StartsWith method.

I hope you find this answer useful :)


这篇关于C#输入表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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