0

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!

2
  • Use 1 character class containing the ranges/symbols you allow. Use anchors, use a quantifier. Commented May 28, 2016 at 23:09
  • 1
    Possible duplicate of php regex validation Commented May 28, 2016 at 23:28

3 Answers 3

1

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"
----------------------------------------------------------------------
Sign up to request clarification or add additional context in comments.

Comments

0

"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.

1 Comment

Thanks a bunch! This worked. And I also learned and understood a bit more of regex from all the comments so thank you everyone for answering!
0

[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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.