Vlad Eugen Nitu
Vlad Eugen Nitu

Reputation: 305

PHP regex for name verification

So I have this assignment. I have to validate a name input that can contain big and small letters, apostrophe, spaces or comas. So I've come up with this for validation:

$name = $_POST['your_name'];
$regex_name = '/([A-Z]|[a-z])([A-Z][a-z\s])+/';
if (!preg_match($regex_name, $name)) {
    echo '<span style="color:red">Please input a valid name!</span><br />';
}

But it doesn't seem to work fine and I don't know why.

I've read about regex and the rules but I don't get what I'm doing wrong. I've even seen some examples here on stackoverflow but it's still not clear for me.

I think this should validate at least letters but it gives false even for a simple input as 'error'.

Please help!

Upvotes: 0

Views: 111

Answers (3)

miken32
miken32

Reputation: 42716

"Doesn't work fine" is not a helpful description. But I'd guess your problem is likely that you aren't anchoring your query with a start of string (^) and end of string ($). This means you're only matching a subset of characters anywhere in the string.

Also, you didn't say so in the question, but in your code it looks like you want to only allow letters for the first character. This should do the trick for you:

$name = $_POST['your_name'];
$regex_name = "/^[a-z][a-z,' ]+$/i";
if (!preg_match($regex_name, $name)) {
    echo '<span style="color:red">Please input a valid name!</span><br />';
}

I've also used the i modifier at the end of the expression to make the search case-insensitive.

Upvotes: 0

colonelclick
colonelclick

Reputation: 2215

[a-zA-z,' ]

I chose not to use \s because you specified spaces only, not any whitespace character.

This is a great tool to debug regex on the fly.

Rubular

Upvotes: 0

Ro Yo Mi
Ro Yo Mi

Reputation: 15000

Description

^[a-zA-Z'\s,]+$

Regular expression visualization

This regular expression will do the following:

  • validate a given string has only:
    • upper case letters
    • lower case letters
    • spaces
    • commas
    • apostrophe

In the character class I included both a-z and A-Z, and in the example I have the case insensitive flag enabled. So this is a bit redundant.

Example

Live Demo

https://regex101.com/r/rM2iU3/2

Sample text

aaaaaaa
bbbb,bbbb
cccc cccc
ddd'ddddd
fff3ffff
gggggggg

Sample Matches

aaaaaaa
bbbb,bbbb
cccc cccc
ddd'ddddd
gggggggg

Explanation

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  [a-zA-Z'\s,]+            any character of: 'a' to 'z', 'A' to 'Z',
                           ''', whitespace (\n, \r, \t, \f, and " "),
                           ',' (1 or more times (matching the most
                           amount possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of a
                           "line"
----------------------------------------------------------------------

Upvotes: 1

Related Questions