Don't forget about names like:
- Mathias d'Arras
- Martin Luther King, Jr.
- Hector Sausage-Hausen
This should do the trick for most things:
/^[a-z ,.'-]+$/i
OR Support international names with super sweet unicode:
/^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžæÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$/u
Don't forget about names like:
- Mathias d'Arras
- Martin Luther King, Jr.
- Hector Sausage-Hausen
This should do the trick for most things:
/^[a-z ,.'-]+$/i
OR Support international names with super sweet unicode:
/^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžæÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$/u
You make false assumptions on the format of first and last name. It is probably better not to validate the name at all, apart from checking that it is empty.
Java Regex to Validate Full Name allow only Spaces and Letters - Stack Overflow
How to use regex to validate only alphabets | OutSystems
Regular expression for name field in javascript validation - Stack Overflow
regular expressions - Best REGEX for first/last name validation? - Salesforce Stack Exchange
Videos
What about:
- Peter Müller
- François Hollande
- Patrick O'Brian
- Silvana Koch-Mehrin
Validating names is a difficult issue, because valid names are not only consisting of the letters A-Z.
At least you should use the Unicode property for letters and add more special characters. A first approach could be e.g.:
String regx = "^[\\p{L} .'-]+$";
\\p{L} is a Unicode Character Property that matches any kind of letter from any language/script. \\p{L} is equivalent to \\p{javaLetter}, and both are equivalent to Character::isLetter.
try this regex (allowing Alphabets, Dots, Spaces):
"^[A-Za-z\s]{1,}[\.]{0,1}[A-Za-z\s]{0,}$" //regular
"^\pL+[\pL\pZ\pP]{0,}$" //unicode
This will also ensure DOT never comes at the start of the name.
Yes! Let's validate some names with RegEx.
After all, we know that all people must have a first and last name, right? And no single person has more than three or four names total? And no doubt the same person will forever be identifiable by the same name?
Plus, we know that no modern culture uses patronymic naming and people in the same nuclear family must have the same last name, right?
Well, we can at least assume that people do not have single character names, right? And there are no names that use special characters, symbols, or apostrophes?
I think your choice of RegEx to validate names is missing the point: this is a huge unwieldy problem and, even if you massively restrict the scope of names you allow, you will forever suffer the risk of false negatives and you will be turning away people from other cultures and languages. In other words, I don't think that even attempting to validate names is worth your time.
The reason it's breaking on names like McGowan is because you're second character class doesn't allow for Capitalized characters.
Use the below regex to match names with Capitalization after the first character.
([A-Z][a-zA-Z]*)
You can use
^A-Z[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[A-Za-z]*(?:\\h+[A-Z][A-Za-z]*)*$";
var pattern = Pattern.compile("^((?=.{1,29}$)[A-Z]\\w*(\\s[A-Z]\\w*)*)$");
var matcher = pattern.matcher("Multiple Words With One Space Separator");
System.out.println(matcher.matches()); // false
matcher = pattern.matcher("Multiple Words");
System.out.println(matcher.matches()); // true
In this case, your code isn't working 'cause your regex was set as a string in the line:
var reg="/^[a-zA-Z]*$/";
The regex was suposed to be set without quotes:
var reg=/^[a-zA-Z]*$/;
I suggest that instead of the method 'match', you use:
else if (!reg.test(x)){
This is more performatic and return a boolean value.
Instead of...
var reg = "/^[a-zA-Z]*$/"; // produces a **string**
You should omit the surrounding quotes:
var reg = /^[a-zA-Z]*$/; // produces a **Regex object**
The second uses javascript inline Regex syntax, with the slashes functioning as delimiters.
Furthermore you can use the simple .test() function to check if a string conforms to a Regex or not.
Demo:
var reg = /^[a-zA-Z]*$/;
console.log(reg.test("ABcd"));
console.log(reg.test("123"));
You may use any of these 2 variants:
/^[A-Z]+$/i
/^[A-Za-z]+$/
to match an input string of ASCII alphabets.
[A-Za-z]will match all the alphabets (both lowercase and uppercase).^and$will make sure that nothing but these alphabets will be matched.
Code:
preg_match('/^[A-Z]+$/i', "abcAbc^Xyz", $m);
var_dump($m);
Output:
array(0) {
}
Test case is for OP's comment that he wants to match only if there are 1 or more alphabets present in the input. As you can see in the test case that matches failed because there was ^ in the input string abcAbc^Xyz.
Note: Please note that the above answer only matches ASCII alphabets and doesn't match Unicode characters. If you want to match Unicode letters then use:
/^\p{L}+$/u
Here, \p{L} matches any kind of letter from any language
If you need to include non-ASCII alphabetic characters, and if your regex flavor supports Unicode, then
\A\pL+\z
would be the correct regex.
Some regex engines don't support this Unicode syntax but allow the \w alphanumeric shorthand to also match non-ASCII characters. In that case, you can get all alphabetics by subtracting digits and underscores from \w like this:
\A[^\W\d_]+\z
\A matches at the start of the string, \z at the end of the string (^ and $ also match at the start/end of lines in some languages like Ruby, or if certain regex options are set).