[英]How can I validate text box input?
I am creating a program and I need to validate my text boxes. 我正在创建一个程序,我需要验证我的文本框。 For the program the user needs to put in a phrase.
对于程序,用户需要输入一个短语。 But I am not sure how to make sure that the user actually entered in a phrase, the phrase isn't (ex.) skldkfdl, or that there isn't a space.
但是我不确定如何确保用户实际输入了一个短语,该短语不是(例如)skldkfdl或没有空格。
You could do a String.Trim() to get rid of trailing whitespaces first... 你可以做一个String.Trim()来摆脱尾随空格...
then do a String.IndexOf(" ") to check for a space. 然后执行String.IndexOf(“”)以检查空格。
If the function returns -1, it means there is no space in the string. 如果函数返回-1,则表示字符串中没有空格。
Running on the assumption that you're using VB.Net - Add an event handler for the event where you want to validate the text, such as when a "Submit" button is clicked. 在假设您使用VB.Net的情况下运行-为要验证文本的事件添加事件处理程序,例如单击“提交”按钮时。 You may want to use a CancelEventHandler, so that you can cancel the click.
您可能要使用CancelEventHandler,以便可以取消单击。 In the event handler, if you're looking for just simple validation, you can use if-statements to check some simple conditions, such as if you just want to check "if input.equals(password)".
在事件处理程序中,如果您只是在寻找简单的验证,则可以使用if语句来检查一些简单的条件,例如是否只想检查“ if input.equals(password)”。 Look here for an example of using CancelEventHandler
在这里查看使用CancelEventHandler的示例
If you're looking for some more complex validation, you'll want to use regular expressions. 如果您要寻找一些更复杂的验证,则需要使用正则表达式。 This page might help get you started
此页面可能有助于您入门
Checking to see if something is "a phrase", as in, proper English, would be very difficult. 用正确的英语来检查某物是否为“短语”会非常困难。 You would need to make sure that all of the words are in the dictionary, and then you would need to check for proper grammar, which is incredibly complex, given English grammar rules.
您需要确保所有单词都在词典中,然后您需要检查正确的语法,在给定英语语法规则的情况下,这是非常复杂的。 You may want to simplify your approach, depending on your problem.
您可能要根据您的问题简化方法。 For example, maybe just check that no weird characters are used, that there is more than one space, and that each word contains a vowel.
例如,也许只是检查没有使用任何奇怪的字符,是否有多个空格,并且每个单词都包含一个元音。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.