简体   繁体   中英

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.

Strings in Java

You could do a String.Trim() to get rid of trailing whitespaces first...

then do a String.IndexOf(" ") to check for a space.

If the function returns -1, it means there is no space in the string.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM