validating password

Thread Solved

Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

validating password

 
0
  #1
Dec 9th, 2008
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!
  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: validating password

 
0
  #2
Dec 9th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

Re: validating password

 
0
  #3
Dec 9th, 2008
Wow and thx! - that was a quick response.

But, I'm confused. Can you show me with one of the validations?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,190
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 483
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: validating password

 
0
  #4
Dec 9th, 2008
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
  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. }
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 52
Reputation: clueless101 is an unknown quantity at this point 
Solved Threads: 0
clueless101 clueless101 is offline Offline
Junior Poster in Training

Re: validating password

 
0
  #5
Dec 9th, 2008
Originally Posted by peter_budo View Post
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
  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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC