Results 1 to 12 of 12
  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default 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.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default 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.

  3. #3
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: text field rule requirement

    Quote Originally Posted by wsaryada View Post
    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.
    is there any other way to about this?

    If not, how would I accept only caps with numbers and .'s. I haven't really got onto regex yet. I know the caps part owuld be (A-Z)* but I don't understand how to put this as a whole string including digits and the full stops.

  4. #4
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: text field rule requirement

    bump - can anyone explain how I could go about making sure the user enters with capitals only without regex?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: text field rule requirement

    Why not simply use a for loop, looping through each character of the String obtained and checking each character?

  6. #6
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default 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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: text field rule requirement

    Quote Originally Posted by stuckonjava View Post
    How do I check if something is uppercase?
    That's a good question, and I know of two good answers for it:
    1. You could use < and > operators with char, but even better,
    2. 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.

  8. #8
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: text field rule requirement

    Quote Originally Posted by Fubarable View Post
    That's a good question, and I know of two good answers for it:
    1. You could use < and > operators with char, but even better,
    2. 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.
    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

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  10. #10
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default 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.

  11. #11
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: text field rule requirement

    does anyone know how I can only accept the symbol "." in my search?

  12. #12
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

Similar Threads

  1. Text Field Validation HELP
    By Richard5324 in forum New To Java
    Replies: 1
    Last Post: 11-17-2011, 05:29 PM
  2. text field limit
    By aizen92 in forum New To Java
    Replies: 4
    Last Post: 04-03-2011, 09:52 AM
  3. get numeric value from a text field
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 03:19 AM
  4. Regarding Text Field
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-05-2008, 11:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •