Java Regex to Validate Full Name Allow Only Spaces and Letters

Regular expression to validate a name

You can use

^[A-Z](?=.{1,29}$)[A-Za-z]*(?:\h+[A-Z][A-Za-z]*)*$

The pattern matches:

  • ^ Start of string
  • [A-Z] Match an uppercase char A-Z
  • (?=.{1,29}$) Assert 1-29 chars to the right till the end of the string
  • [A-Za-z]* Optionally match a char A-Za-z
  • (?:\h+[A-Z][A-Za-z]*)* Optionally repeat 1+ horizontal whitespace chars followed by again an uppercase char A-Z and optional chars A-Za-z
  • $ End of string

Regex demo

In Java with the doubled backslashes

String regex = "^[A-Z](?=.{1,29}$)[A-Za-z]*(?:\\h+[A-Z][A-Za-z]*)*$";

Regex for names validation allow only letters and spaces

To match the terms:

  • Expression can start only with a letter
  • Expression can contain letters or spaces

You need to use this regex expression:

/^[a-z][a-z\s]*$/

So in your js it should be:

jQuery.validator.addMethod("letterswithspace", function(value, element) {
return this.optional(element) || /^[a-z][a-z\s]*$/i.test(value);
}, "letters only");

Explanation

  1. ^[a-z] means start with one letter
  2. [a-z\s]*$ means after accept zero or more letters or spaces

Valid sentence

If you want a valid sentence structure:

  • Expression can start or end only with a letter
  • Expression cannot contain consecutive spaces

use:

/^([a-z]+\s)*[a-z]+$/

By the way


  1. These regex expressions do not accept capital letters. To add capital letters support instead of a-z use a-zA-Z

Regex to accept only alphabets and spaces and disallowing spaces at the beginning and the end of the string

For me the only logical way to do this is:

^\p{L}+(?: \p{L}+)*$

At the start of the string there must be at least one letter. (I replaced your [a-zA-Z] by the Unicode code property for letters \p{L}). Then there can be a space followed by at least one letter, this part can be repeated.

\p{L}: any kind of letter from any language. See regular-expressions.info

The problem in your expression ^(?!\s*$) is, that lookahead will fail, if there is only whitespace till the end of the string. If you want to disallow leading whitespace, just remove the end of string anchor inside the lookahead ==> ^(?!\s)[-a-zA-Z ]*$. But this still allows the string to end with whitespace. To avoid this look back at the end of the string ^(?!\s)[-a-zA-Z ]*(?<!\s)$. But I think for this task a look around is not needed.

Regex for validating a name of this particular format

use:

^[^\s][ A-Za-z-'@.,/]*$
  • ^[^\s] checks that there is no whitespace at the beginning
  • A-Za-z accepts all the alphabets in the given string
  • -'@.,/ accepts only these special characters
  • * matches 0 or more preceding token

java regex for alpha and spaces is including [ ] \

You have a typo in your regex:

"[a-zA-z\\s]*"

[A-z] matches all the uppercase and lowercase letters plus [, ], ^, _, backslash and backtick, whose code points happen to lie between Z and a.

Also, I agree with @Ωmega that you probably should be using an actual space character instead of \s:

"[a-zA-Z ]*"

The anchors (^ and $) aren't necessary since you're using the matches() method, which automatically anchors the match at both ends. They don't hurt anything though, and they do help communicate your intent.



Related Topics



Leave a reply



Submit