| | |
validating password
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 52
Reputation:
Solved Threads: 0
•
•
•
•
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)
import javax.swing.*; public class pwVer { public static void main(String[] args) { String input; input = JOptionPane.showInputDialog("Please enter password"); // Validate user's input if (isValid(input)) { JOptionPane.showMessageDialog(null, "Your password is valid!"); } else { JOptionPane.showMessageDialog(null, "Your password is invalid " + "valid password criteria."); } System.exit(0); } /** The isValid method determines password validity. @param passWord The String to test. @return true if valid, or else false. */ private static boolean isValid(String pw) { boolean isvalid = true; int i = 0; //Test the length if (pw.length() != 5) isvalid = false; //Test for lowercase while (isvalid && i < 5) { if (!Character.isLowerCase(pw.charAt(i))) isvalid = false; i++; } //Test for uppercase while (isvalid && i < 5) { if (!Character.isUpperCase(pw.charAt(i))) isvalid = false; i++; } //Test for digit while (isvalid && i < 5) { if (!Character.isDigit(pw.charAt(i))) isvalid = false; i++; } return isvalid; } }
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 16
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.
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.
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
Also you may consider something like this
java Syntax (Toggle Plain Text)
import javax.swing.*; public class PasswordCheck { public static void main(String[] args) { String input; input = JOptionPane.showInputDialog("Please enter password"); // Validate user's input if (isValid(input)) { JOptionPane.showMessageDialog(null, "Your password is valid!"); } else { JOptionPane.showMessageDialog(null, "Your password is invalid " + "valid password criteria."); } System.exit(0); } /** The isValid method determines password validity. @param passWord The String to test. @return true if valid, or else false. */ private static boolean isValid(String pw) { if(checkLength(pw) && lowCaseCheck(pw) && upCaseCheck(pw) && digCheck(pw)) { return true; } return false; } private static boolean checkLength(String psw) { if (psw.length() == 5){return true;} return false; } private static boolean lowCaseCheck(String psw) { for(int i = 0; i < psw.length(); i++) { if(Character.isLowerCase(psw.charAt(i))) { return true; } } return false; } private static boolean upCaseCheck(String psw) { for(int i = 0; i < psw.length(); i++) { if(Character.isUpperCase(psw.charAt(i))) { return true; } } return false; } private static boolean digCheck(String psw) { for(int i = 0; i < psw.length(); i++) { if(Character.isDigit(psw.charAt(i))) { return true; } } return false; } }
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Mar 2008
Posts: 52
Reputation:
Solved Threads: 0
•
•
•
•
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)
import javax.swing.*; public class PasswordCheck { public static void main(String[] args) { String input; input = JOptionPane.showInputDialog("Please enter password"); // Validate user's input if (isValid(input)) { JOptionPane.showMessageDialog(null, "Your password is valid!"); } else { JOptionPane.showMessageDialog(null, "Your password is invalid " + "valid password criteria."); } System.exit(0); } /** The isValid method determines password validity. @param passWord The String to test. @return true if valid, or else false. */ private static boolean isValid(String pw) { if(checkLength(pw) && lowCaseCheck(pw) && upCaseCheck(pw) && digCheck(pw)) { return true; } return false; } private static boolean checkLength(String psw) { if (psw.length() == 5){return true;} return false; } private static boolean lowCaseCheck(String psw) { for(int i = 0; i < psw.length(); i++) { if(Character.isLowerCase(psw.charAt(i))) { return true; } } return false; } private static boolean upCaseCheck(String psw) { for(int i = 0; i < psw.length(); i++) { if(Character.isUpperCase(psw.charAt(i))) { return true; } } return false; } private static boolean digCheck(String psw) { for(int i = 0; i < psw.length(); i++) { if(Character.isDigit(psw.charAt(i))) { return true; } } return false; } }
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.
![]() |
Similar Threads
- Validating radio button selection for login redirect (JavaScript / DHTML / AJAX)
- Password Storage System. (Python)
- Validate username/password vs SQL Db (C#)
- good way of validating login info vs SQL D/B (ASP.NET)
- Validating a User Imput with SQL Database (Login) (C#)
- JavaScript -- Validating an email address (JavaScript / DHTML / AJAX)
Other Threads in the Java Forum
- Previous Thread: Get File creation Date
- Next Thread: Retrive Database content to JTextField and write JTextField Data to database
| Thread Tools | Search this Thread |
6 @param actuate android api applet application arc array arrays automation balls binary bluetooth bold business byte c++ chat class client code codesnippet collections compare component coordinates database defaultmethod detection doctype dragging ebook eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list map method methods mobile mysql netbeans newbie nextline parameter php pong problem program programming project recursion recursive scanner sell server set sms sort sql string sun swing swt terminal threads tree web websites windows






