Java Regex to Validate Full Name allow only Spaces and Letters

前端 未结 14 2349
抹茶落季
抹茶落季 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:08

    For those who use java/android and struggle with this matter try:

    "^\\p{L}+[\\p{L}\\p{Z}\\p{P}]{0,}"
    

    This works with names like

    • José Brasão
    0 讨论(0)
  • 2020-11-28 05:08

    check this out.

    String name validation only accept alphabets and spaces
    public static boolean validateLetters(String txt) {
    
        String regx = "^[a-zA-Z\\s]+$";
        Pattern pattern = Pattern.compile(regx,Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(txt);
        return matcher.find();
    
    }
    
    0 讨论(0)
  • To validate for only letters and spaces, try this

    String name1_exp = "^[a-zA-Z]+[\-'\s]?[a-zA-Z ]+$";
    
    0 讨论(0)
  • 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();
    
    }
    
    0 讨论(0)
  • 2020-11-28 05:16

    Validates such values as: "", "FIR", "FIR ", "FIR LAST"

    /^[A-z]*$|^[A-z]+\s[A-z]*$/
    
    0 讨论(0)
  • 2020-11-28 05:16

    This works for me with validation of bootstrap

    $(document).ready(function() {
    $("#fname").keypress(function(e) {
    var regex = new RegExp("^[a-zA-Z ]+$");
    var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
    if (regex.test(str)) {
     return true;
    }
    
    0 讨论(0)
提交回复
热议问题