Thursday 21 December 2017

How to write a regex to validate the password | Regex in java


Click here to watch on Youtube :
https://www.youtube.com/watch?v=Bwc6apOcMlA&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
How to write a regex to validate the password | Regex in java

PasswordValidator.java

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * How to validate password with regular expression
 *
 */

public class PasswordValidator
{
    private Pattern pattern;
    private Matcher matcher;

    private static final String PASSWORD_REGEX =
                      "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";

    public PasswordValidator()
    {
        pattern = Pattern.compile(PASSWORD_REGEX);
    }

    /**
     * Validate password with regular expression
     *
     * @param password  password for validation
     * @return true valid password, false invalid password
     */

    public boolean validate(final String password)
    {
        matcher = pattern.matcher(password);
        return matcher.matches();
    }

}

RegexDemo.java

/**
 *
 * How to validate password with regular expression
 *
 */

public class RegexDemo
{

    public static void main(String[] args)
    {
        PasswordValidator passwordValidator = new PasswordValidator();
       
        System.out.println("'peter1AB@' is valid? = "+passwordValidator.validate("peter1AB@"));
        System.out.println("'peter4COW$' is valid?  = "+passwordValidator.validate("peter4COW$"));
        System.out.println("'peter78Dog%#' is valid?  = "+passwordValidator.validate("peter78Dog%#"));
       
        System.out.println("--------------------------------------------------");
       
        /*
         *  too short, minimum 6 characters
         */

        System.out.println("'p1AB@' is valid? = "+passwordValidator.validate("p1AB@"));
       
        /*
         *  uppercase characters is required
         */

        System.out.println("'peter1@' is valid? = "+passwordValidator.validate("peter1@"));
       
       
        /*
         *  special symbol “*" is not allow here
         */

        System.out.println("'peterAB2*' is valid? = "+passwordValidator.validate("peterAB2*"));
       
        /*
         *  digit is required
         */

        System.out.println("'peterAB$' is valid? = "+passwordValidator.validate("peterAB$"));
       
       
        /*
         *  lower case character is required
         */

        System.out.println("'PETER2$' is valid? = "+passwordValidator.validate("PETER2$"));
       
       
    }

}

Output

'peter1AB@' is valid? = true
'peter4COW$' is valid?  = true
'peter78Dog%#' is valid?  = true
--------------------------------------------------
'p1AB@' is valid? = false
'peter1@' is valid? = false
'peterAB2*' is valid? = false
'peterAB$' is valid? = false
'PETER2$' is valid? = false

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_password.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_password

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/576579fab6c376d5ecd7cb5b7402b7fd1a7c4ae3/BasicJava/RegexDemo_password/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment