如何验证文本框输入?

时间:2021-08-03 22:16:40

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,或者没有空格。

2 个解决方案

#1


Strings in Java

Java中的字符串

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,则表示字符串中没有空格。

#2


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. You may want to use a CancelEventHandler, so that you can cancel the click. 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)". Look here for an example of using 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.

在假设您正在使用VB.Net的情况下运行 - 为要验证文本的事件添加事件处理程序,例如单击“提交”按钮时。您可能想要使用CancelEventHandler,以便取消单击。在事件处理程序中,如果您正在寻找简单的验证,则可以使用if语句来检查一些简单的条件,例如,如果您只想检查“if input.equals(password)”。在这里查看使用CancelEventHandler的示例如果您正在寻找更复杂的验证,那么您将需要使用正则表达式。这个页面可能会帮助你开始检查是否有什么东西是“一个短语”,就像在适当的英语中一样,会非常困难。您需要确保所有单词都在字典中,然后根据英语语法规则,您需要检查正确的语法,这非常复杂。您可能希望根据您的问题简化您的方法。例如,可能只是检查没有使用奇怪的字符,有多个空格,并且每个单词都包含一个元音。

#1


Strings in Java

Java中的字符串

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,则表示字符串中没有空格。

#2


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. You may want to use a CancelEventHandler, so that you can cancel the click. 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)". Look here for an example of using 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.

在假设您正在使用VB.Net的情况下运行 - 为要验证文本的事件添加事件处理程序,例如单击“提交”按钮时。您可能想要使用CancelEventHandler,以便取消单击。在事件处理程序中,如果您正在寻找简单的验证,则可以使用if语句来检查一些简单的条件,例如,如果您只想检查“if input.equals(password)”。在这里查看使用CancelEventHandler的示例如果您正在寻找更复杂的验证,那么您将需要使用正则表达式。这个页面可能会帮助你开始检查是否有什么东西是“一个短语”,就像在适当的英语中一样,会非常困难。您需要确保所有单词都在字典中,然后根据英语语法规则,您需要检查正确的语法,这非常复杂。您可能希望根据您的问题简化您的方法。例如,可能只是检查没有使用奇怪的字符,有多个空格,并且每个单词都包含一个元音。