Java Regex to Validate Full Name allow only Spaces and Letters

前端 未结 14 2486
抹茶落季
抹茶落季 2020-11-28 04:50

I want regex to validate for only letters and spaces. Basically this is to validate full name. Ex: Mr Steve Collins or Steve Collins I tried this regex.

14条回答
  •  迷失自我
    2020-11-28 05:16

    @amal. This code will match your requirement. Only letter and space in between will be allow, no number. The text begin with any letter and could have space in between only. "^" denotes the beginning of the line and "$" denotes end of the line.

    public static boolean validateLetters(String txt) {
    
        String regx = "^[a-zA-Z ]+$";
        Pattern pattern = Pattern.compile(regx,Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(txt);
        return matcher.find();
    
    }
    

提交回复
热议问题