Results 1 to 12 of 12
Thread: text field rule requirement
- 03-24-2012, 04:24 AM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 151
- Rep Power
- 10
text field rule requirement
I need a text field where when the return button is clicked a JOptionPane appears whenever the text field contains more than 8 characters, has lower case characters and contains something other than a digit or a "."
They are allowed to type in lower case, and use anything other than digits and the . but is this is done the error will pop up.
So far I have for the length:
String search = fieldOne.getText();
if (search.length()>8){
JOptionPane
.showMessageDialog(this,
"Input is greater then 8 characters, please check and try again");
I have no idea how to make sure they type in capitals also no idea how to find if they used for example a !. would I need to use a chartAt(i) for loop to check for every other symbol apart from ".", this seems a bit long and there must be other ways.
- 03-24-2012, 05:26 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
Re: text field rule requirement
Have you considered using a regex to validate the user input? You can define the pattern that you'll accept in your text field using a regular expression.
Website: Learn Java by Examples
- 03-24-2012, 05:36 AM #3
Senior Member
- Join Date
- Jan 2012
- Posts
- 151
- Rep Power
- 10
Re: text field rule requirement
- 03-24-2012, 04:09 PM #4
Senior Member
- Join Date
- Jan 2012
- Posts
- 151
- Rep Power
- 10
Re: text field rule requirement
bump - can anyone explain how I could go about making sure the user enters with capitals only without regex?
-
Re: text field rule requirement
Why not simply use a for loop, looping through each character of the String obtained and checking each character?
- 03-24-2012, 05:16 PM #6
Senior Member
- Join Date
- Jan 2012
- Posts
- 151
- Rep Power
- 10
Re: text field rule requirement
How do I check if something is uppercase? So far:
for(int i =0; i < name.length();i++){
if (name.charAt(i).//don't know what to put here)
{
JOptionPane
.showMessageDialog(this,
"Input is greater then 8 characters, please check and try again");
}}
I don't know what to put here.
-
Re: text field rule requirement
That's a good question, and I know of two good answers for it:
- You could use < and > operators with char, but even better,
- The Character class has a static method that can be used to detect if a char is uppercase or lowercase. Please have a look at the Character class API and look for a method that starts with isUpper... or isLower... and you will find something that you can use.
- 03-24-2012, 05:55 PM #8
Senior Member
- Join Date
- Jan 2012
- Posts
- 151
- Rep Power
- 10
Re: text field rule requirement
Thanks for this. I now have this is :
int upperCase = 0;
for(int i = 0; i<s.length();i++){
char ch = s.charAt(i);
if(Character.isUpperCase(ch))
{
upperCase++;
}
}
if(upperCase < s.length()){
JOptionPane
.showMessageDialog(this,
"You need to type every letter in caps");
}
if (s.length()>8){
JOptionPane
.showMessageDialog(this,
"Input is greater then 8 characters, please check and try again");
}
The thing is now, if some types hhhhhhhhhhhhhhhhhhh into the search box, first the caps message pops up and after closing it, the greater than 8 message shows up.
How can I merge it so that here is one message for any error saying for eg, either you have not entered all caps or you have entered mroe than 8 charcters.
Thanks
-
Re: text field rule requirement
If all you want to do is to check the input and then if it is not valid, show a JOptionPane that states this, then perhaps you want to use a boolean, say called inputCorrect, and set it equal to true initially. Then as you test the input String, set it to false if the input fails any test. Then after all of your test code has been completed check the value of inputCorrect and display a JOptionPane informing the user that the input was invalid. If on the other hand you need to show a specific JOptionPane that describes the reason that the input was bad, you'll need some slightly more complex logic.
- 03-24-2012, 06:16 PM #10
Senior Member
- Join Date
- Jan 2012
- Posts
- 151
- Rep Power
- 10
Re: text field rule requirement
for my input I have the requirements:
1. Every character in the search must be upper case - done
2. The search string can be no longer than 8 characters - done
3. Can contain digits , periods and nothing else(apart from the uppercase letters) - How do I go about doing thing this, I don't need to make any changes for the digits but what if the user enters a ^. How can I make sure there isn't any other symbol? Do i need to make a counter like I did for uppercase searching through every single symbol because there are quite a few!? It seems a bit long and tedious and I'm sure there must be other ways around this.
- 03-24-2012, 07:09 PM #11
Senior Member
- Join Date
- Jan 2012
- Posts
- 151
- Rep Power
- 10
Re: text field rule requirement
does anyone know how I can only accept the symbol "." in my search?
- 03-24-2012, 11:26 PM #12
Senior Member
- Join Date
- Jan 2012
- Posts
- 151
- Rep Power
- 10
Similar Threads
-
Text Field Validation HELP
By Richard5324 in forum New To JavaReplies: 1Last Post: 11-17-2011, 06:29 PM -
text field limit
By aizen92 in forum New To JavaReplies: 4Last Post: 04-03-2011, 10:52 AM -
get numeric value from a text field
By Lehane_9 in forum New To JavaReplies: 2Last Post: 06-14-2008, 04:19 AM -
Regarding Text Field
By adeeb in forum AWT / SwingReplies: 1Last Post: 06-06-2008, 12:01 AM
Bookmarks