views:

1390

answers:

4

I am doing 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.

+2  A: 

First, you need to define precisely what you mean by "phrase". What is the set of acceptable inputs, and what algorithm will you use to determine that an input is valid? You need to define your problem more.

McWafflestix
A: 

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.

Jon
A: 

[edit: wrong language]

A: 

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.

T.R.