Skip to content

Instantly share code, notes, and snippets.

@hitenpratap
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitenpratap/28166ffe80215cf27e96 to your computer and use it in GitHub Desktop.
Save hitenpratap/28166ffe80215cf27e96 to your computer and use it in GitHub Desktop.
Validate password in java that must have atleat 1 uppercase char, 1 number and 1 special char in it and can be of 6 to 20 character in size.
package com.hprog99;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestPasswordValidator {
private static Pattern passPattern =
Pattern.compile("((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%_*]).{6,20})");
public static boolean validatePassword(String pwd){
Matcher matcher = passPattern.matcher(pwd);
return matcher.matches();
}
public static void main(String pwds[]){
System.out.println("Admin2@ - "
+validatePassword("Admin2@"));
System.out.println("123HNG89# - "
+validatePassword("123HNG89#"));
System.out.println("f54A# - "
+validatePassword("f54A#"));
}
}
//Output:
//1. Admin2@ - true
//2. 123HNG89# - false
//3. f54A# - false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment