Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Verify Password

  1. #1
    hypes057 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default Verify Password

    I am trying to figure out how I can make my script verify a "password" (not a real password) is between 6 and 10 characters long, contains at least one letter, and contains at least one digit in addition to prompting the user until the password is ok. After that I need to make sure that the password is then entered properly twice. This is what I have so far:

    Java Code:
    import javax.swing.*;
    public class PasswordCheck
    {
    	public static void main(String[] args)
    	{
    		String name, saveOriginalName;
    		int stringLength;
    		int i;
    		char c;
    		name = JOptionPane.showInputDialog(null, "Please enter your password");
    		saveOriginalName = name;
    		stringLength = name.length();
    		for (i=0; i < stringLength; i++)
    		{
    			c = name.charAt(i);
    			if(i == 0)
    				{
    				c = Character.toUpperCase(c);
    				name = c + name.substring(1, stringLength);
    				}
    			else
    				if(name.charAt(i) == ' ')
    				{
    				++i;
    				c = name.charAt(i);
    				c = Character.toUpperCase(c);
    				name = name.substring(0, i) + c +
    				name.substring(i + 1, stringLength);
    				}
    			}
    		JOptionPane.showMessageDialog(null, "Original name was " + 
    		saveOriginalName + "\nRepaired name is " + name);
    		System.exit(0);
    		}
    	}
    I am having difficulty specifying 6 digits and prompting for numbers as well. In addition, I cant get to the last part. Could one of the Java Saavy please help me out?

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

    Default

    I am trying to figure out how I can make my script verify a "password"
    is between 6 and 10 characters long
    You get a String, you know how to check a String's length, so solved.

    contains at least one letter, and contains at least one digit
    Yep, your for-loop idea is good here. Character class has methods that may help you here.

    in addition to prompting the user until the password is ok.
    In other words, this loops for an unknown number of times until some conditions are true. Which is the best type of loop for this? You should then think what code should go into the loop (hint: most of it). You might want to have a boolean variable for each condition above (lengthOK, containsChar, containsLetter) and checking that all are true before exiting your loop.

    After that I need to make sure that the password is then entered properly twice.
    hint: don't forget that you compare Strings with the equals method not with ==.

    This is what I have so far:...
    I'd scrap this and work out the logic first on paper before trying to type in the code. Otherwise you'll end up with a hit-or-miss type of program.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And also try to implement in different methods, keep in head about OO concepts. Well organized code really helpful to you, build the strong design for this.

    Check the length in different method, and use a boolean value to verify. Something like this as an example.

    Java Code:
        private boolean IsValidLength(String pwd) {
            return ((pwd.length() >= 6) || pwd.length() <= 8) ? true : false;
        }

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    For checking atleast one character,digit etc u can make use of regex patterns.

    For checking the password entered more than once then u can a counter.If the counter reaches 2 break and come out .
    Ramya:cool:

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes there are lots of approaches to do this. But looking at the OPs' code, I don't think he has an idea about the regular expressions, and anyway it's very effective way to validate the password.

  6. #6
    hypes057 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default

    Thanks everyone for your responses! Unfortunately, they don't help me out as much as I'd like cuz my issue is more with the syntax than the mechanics. I can figure it out on a flowchart and/or paper just implementing it is hard when I cant figure out the language. Anyone willing to point me in the proper direction with an example?

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Okay, tell what's your exact requirement? Type the password twice and want to validate it, is it? That's only you want to do?

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Here is a simple example, please look at the comments as well.

    Java Code:
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author Eranga Thennakoon
     */
    public class PasswordValidator {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            new PasswordValidator().Validate();
        }
    
        private void Validate() {
            String strInitial, strSecondry = null;
            
            // Get the password from the user for the first time
            strInitial = JOptionPane.showInputDialog(null, "Please enter the password",
                    "Enter your password");
            
            // Validation based or your conditions
            if(IsValidLength(strInitial) && IsValidPattern(strInitial)) {
                // Since the validations are okay, ask from the user to re-type the 
                // password
                strSecondry = JOptionPane.showInputDialog(null, "Please re-typs the " +
                        "password again", "Enter your passward again");
                
                // Match both user inputs
                if(strInitial.equals(strSecondry)) {
                    JOptionPane.showMessageDialog(null, "Congardulations, your passwards are match");
                }
                else {
                    JOptionPane.showMessageDialog(null, "Your passwords are not match");
                }
            }
            else {
                // Validation faild
                JOptionPane.showMessageDialog(null, "Your password in invalid");
            }
        }
    
        private boolean IsValidLength(String pwd) {
            // Check the length of the password
            return ((pwd.length() >= 6) || pwd.length() <= 8) ? true : false;
        }
    
        private boolean IsValidPattern(String str) {
            for(int index = 0; index < str.length(); index++) {
                try {
                    // Valid only if a digit contain in the string
                    Integer val = Integer.parseInt(Character.toString(str.charAt(index)));
                    return true;
                }
                catch(NumberFormatException ex) {
                    // Character found in this case
                }
            }
            return false;
            // This same logic can be implemented using regex in Java. But avoid
            // that at the moment
        }
    
    }

  9. #9
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    for validating a password with regex look here Regex (regular expression) password check with Java | Cafe Webmaster. if you want to check for "at least one letter" instead of "at least one upper and one lower letter" replace

    ((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{6,})

    with

    "((?=.*\\d)(?=.*[a-zA-Z]).{6,})"

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Zombie alert

    db
    Fubarable likes this.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Java RE SE does not install/verify
    By jlbm in forum New To Java
    Replies: 1
    Last Post: 03-20-2009, 08:45 PM
  2. Verify if a file as been, for sure, printed
    By pjmorce in forum Advanced Java
    Replies: 0
    Last Post: 11-20-2008, 10:45 AM
  3. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum New To Java
    Replies: 2
    Last Post: 11-14-2008, 07:53 PM
  4. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 0
    Last Post: 11-14-2008, 01:22 PM
  5. How to check password of a jsp/html with the password of Database(mysql) #1
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 2
    Last Post: 11-14-2008, 01:11 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
  •