Last active
August 29, 2015 14:17
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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