简体   繁体   English

如何验证文本框输入?

[英]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或没有空格。

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

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.

相关问题 如何使用控件在组框中验证文本框 - How do I validate text boxes with in a a group box using control 如何删除超过一定字符长度的所有单词(在输入框中输入文字)? - How can I delete all words above a certain character length (in text from an input box)? 如何将文本框中的输入用作可以在浏览器上打开的 url (vb.net) - How do I use the input from a text box as a url that can be opened on a browser (vb.net) 验证文本框输入的数字范围为1-100 - Validate text box input for numeric range 1-100 如何创建用户可以输入文本的可视化基本对话框 - How to create a visual basic dialog box that users can input text 如何使用文本框作为MySql查询的用户输入,然后在另一个文本框中显示结果? - How could I use a text box for user input as a MySql query, then display the results in another text box? 如何在visual basic的文本框中搜索文本 - how can i search text within the text box in visual basic 如何将用户输入的内容从文本框写入asp.net/html中的web.config文件? - How can i write a user's input from a text box to the web.config file in asp.net / html? 如何计算文本框中的行数? - How can I calculate the number of lines in a text box? 如何验证用户输入? - How do I validate user input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM