943,949 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 623
  • Java RSS
Dec 9th, 2008
0

validating password

Expand Post »
Quote ...
My code asks a user for a password. To properly validate the user's entry, it must include the following requirements:
1)have a length of 5
2)have an uppercase character
3)have a lowercase character
4)have a digit

My loop only works for the lowercase and length of 5 requirements. Can someone help me with validating uppercase and digit? Thanks in advance for any/all suggestions!
Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2.  
  3. public class pwVer
  4. {
  5. public static void main(String[] args)
  6. {
  7. String input;
  8.  
  9. input = JOptionPane.showInputDialog("Please enter password");
  10.  
  11. // Validate user's input
  12. if (isValid(input))
  13. {
  14. JOptionPane.showMessageDialog(null, "Your password is valid!");
  15. }
  16. else
  17. {
  18. JOptionPane.showMessageDialog(null, "Your password is invalid " +
  19. "valid password criteria.");
  20. }
  21. System.exit(0);
  22. }
  23. /**
  24. The isValid method determines password validity.
  25. @param passWord The String to test.
  26. @return true if valid, or else false.
  27. */
  28.  
  29. private static boolean isValid(String pw)
  30. {
  31. boolean isvalid = true;
  32. int i = 0;
  33.  
  34. //Test the length
  35. if (pw.length() != 5)
  36. isvalid = false;
  37.  
  38. //Test for lowercase
  39. while (isvalid && i < 5)
  40. {
  41. if (!Character.isLowerCase(pw.charAt(i)))
  42. isvalid = false;
  43. i++;
  44. }
  45.  
  46. //Test for uppercase
  47. while (isvalid && i < 5)
  48. {
  49. if (!Character.isUpperCase(pw.charAt(i)))
  50. isvalid = false;
  51. i++;
  52. }
  53.  
  54. //Test for digit
  55. while (isvalid && i < 5)
  56. {
  57. if (!Character.isDigit(pw.charAt(i)))
  58. isvalid = false;
  59. i++;
  60. }
  61.  
  62. return isvalid;
  63. }
  64. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
clueless101 is offline Offline
52 posts
since Mar 2008
Dec 9th, 2008
0

Re: validating password

The lowercase part you have actually won't work for all cases...

What I would do is just have a boolean variable for each condition, and each check will set the appropriate variable to true or false. Then at the end just 'and' them all together to get whether it is valid.

So for lowercase, have the isLowercase variable set to false, then loop through and once you find a lowercase character, set it to true. Same goes for uppercase and digit.
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Dec 9th, 2008
0

Re: validating password

Wow and thx! - that was a quick response.

But, I'm confused. Can you show me with one of the validations?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
clueless101 is offline Offline
52 posts
since Mar 2008
Dec 9th, 2008
0

Re: validating password

You been on right track, but you just forgot to set counter after each while loop back to zero. Because of that capital letters and digit is never checked.
Also you may consider something like this
java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2.  
  3. public class PasswordCheck
  4. {
  5. public static void main(String[] args)
  6. {
  7. String input;
  8.  
  9. input = JOptionPane.showInputDialog("Please enter password");
  10.  
  11. // Validate user's input
  12. if (isValid(input))
  13. {
  14. JOptionPane.showMessageDialog(null, "Your password is valid!");
  15. }
  16. else
  17. {
  18. JOptionPane.showMessageDialog(null, "Your password is invalid " +
  19. "valid password criteria.");
  20. }
  21. System.exit(0);
  22. }
  23.  
  24. /**
  25. The isValid method determines password validity.
  26. @param passWord The String to test.
  27. @return true if valid, or else false.
  28. */
  29.  
  30. private static boolean isValid(String pw)
  31. {
  32. if(checkLength(pw) && lowCaseCheck(pw) && upCaseCheck(pw) && digCheck(pw))
  33. {
  34. return true;
  35. }
  36. return false;
  37. }
  38.  
  39. private static boolean checkLength(String psw)
  40. {
  41. if (psw.length() == 5){return true;}
  42. return false;
  43. }
  44.  
  45. private static boolean lowCaseCheck(String psw)
  46. {
  47. for(int i = 0; i < psw.length(); i++)
  48. {
  49. if(Character.isLowerCase(psw.charAt(i)))
  50. {
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56.  
  57. private static boolean upCaseCheck(String psw)
  58. {
  59. for(int i = 0; i < psw.length(); i++)
  60. {
  61. if(Character.isUpperCase(psw.charAt(i)))
  62. {
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68.  
  69. private static boolean digCheck(String psw)
  70. {
  71. for(int i = 0; i < psw.length(); i++)
  72. {
  73. if(Character.isDigit(psw.charAt(i)))
  74. {
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. }
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Dec 9th, 2008
0

Re: validating password

Click to Expand / Collapse  Quote originally posted by peter_budo ...
You been on right track, but you just forgot to set counter after each while loop back to zero. Because of that capital letters and digit is never checked.
Also you may consider something like this
java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2.  
  3. public class PasswordCheck
  4. {
  5. public static void main(String[] args)
  6. {
  7. String input;
  8.  
  9. input = JOptionPane.showInputDialog("Please enter password");
  10.  
  11. // Validate user's input
  12. if (isValid(input))
  13. {
  14. JOptionPane.showMessageDialog(null, "Your password is valid!");
  15. }
  16. else
  17. {
  18. JOptionPane.showMessageDialog(null, "Your password is invalid " +
  19. "valid password criteria.");
  20. }
  21. System.exit(0);
  22. }
  23.  
  24. /**
  25. The isValid method determines password validity.
  26. @param passWord The String to test.
  27. @return true if valid, or else false.
  28. */
  29.  
  30. private static boolean isValid(String pw)
  31. {
  32. if(checkLength(pw) && lowCaseCheck(pw) && upCaseCheck(pw) && digCheck(pw))
  33. {
  34. return true;
  35. }
  36. return false;
  37. }
  38.  
  39. private static boolean checkLength(String psw)
  40. {
  41. if (psw.length() == 5){return true;}
  42. return false;
  43. }
  44.  
  45. private static boolean lowCaseCheck(String psw)
  46. {
  47. for(int i = 0; i < psw.length(); i++)
  48. {
  49. if(Character.isLowerCase(psw.charAt(i)))
  50. {
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56.  
  57. private static boolean upCaseCheck(String psw)
  58. {
  59. for(int i = 0; i < psw.length(); i++)
  60. {
  61. if(Character.isUpperCase(psw.charAt(i)))
  62. {
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68.  
  69. private static boolean digCheck(String psw)
  70. {
  71. for(int i = 0; i < psw.length(); i++)
  72. {
  73. if(Character.isDigit(psw.charAt(i)))
  74. {
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. }
[QUOTE]
Thank you so much Peter_budo! You've clarified alot for me. It never fails, but I get so far with Java and then there's always 1 or 2 things that I don't understand.

Again, thank you for taking the time. It is truly appreciated.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
clueless101 is offline Offline
52 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Get File creation Date
Next Thread in Java Forum Timeline: Retrive Database content to JTextField and write JTextField Data to database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC