Here you can find the source of passwordValidation()
public static String passwordValidation()
//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; } }