Java Regex Password Validate passwordValidation()

Here you can find the source of passwordValidation()

Description

password Validation

License

Open Source License

Declaration

public static String passwordValidation() 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static String passwordValidation() {

        final String password_regex = "^[A-za-z\\d_-]{8,}$";
        Scanner scanner = new Scanner(System.in);
        String string;//from w  ww .  j  av a2  s .c  o m

        Pattern pattern = Pattern.compile(password_regex);

        while (true) {

            System.out.println("enter password: ");
            string = scanner.nextLine();
            Matcher matcher = pattern.matcher(string);

            if (matcher.matches()) {
                break;
            } else {
                System.out.println(
                        "password length should be at least 8 and special character isn`t allowed except _ , -");
            }

        }
        return string;

    }
}

Related

  1. passwordContainInvalidCharacter(String password)
  2. passwordIsValid(String password)
  3. passwordValidation(String password)
  4. passwordValidator(String password)