Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need Regular Expression for password validation

807591Jan 31 2007 — edited Jun 9 2008
Hi,

I am using jdk regular expression to validate the password.

These are the requirements for valid password:
- password length
minimum 4 characters
maximum 8 characters
- at least one character
- at least one number
- can contain any of the following characters
! @ # $ % ^ &

I could able to validate most of my requirements, expect the last one (related to special characters). Can anyone help me:

Am attaching my code here:

- Kishore
public class ValidatePassword {
	public static void main(String args[]) {

		if(args.length < 1) {
			System.out.println("Usage: ValidatePassword <password>");
		}

		String password = args[0];

		System.out.println( password + " is : "
				+ (password.matches("^([\\p{Alnum}]*)$")
				&& password.length() >= 4
				&& password.length() <= 6));
				
		//I would like to know how to use the range option {4,6} is not working in able example
		//and also would like to know how to validate only !@#$%^ characters along with alphanumerics
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 7 2008
Added on Jan 31 2007
22 comments
2,086 views