Jump to content

Weird regex warning...


yoursurrogategod
 Share

Recommended Posts

Hi.

 

I have this bit of code:

 

 elseif(!preg_match("^[A-Z]'?[- a-zA-Z]( [a-zA-Z])*$", $_POST['lastNameForm']))

 

Now, when I run it, I get this error:

Warning: preg_match(): No ending delimiter '^' found in C:\xampp\htdocs\user_personal_info.php on line 21

 

Warning: preg_match(): No ending delimiter '^' found in C:\xampp\htdocs\user_personal_info.php on line 30

 

Why am I getting this error? I want to check whether the user correctly entered only letters (an apostrophe is permissible as well) for first/lastname.

Link to comment
Share on other sites

Unlike in POSIX (ereg* functions), PCRE (preg*) regexes need delimiters around the expression, mostly to separate it from any flags (case-insensitivity and such).

 

Often you can simply add a character at the front and back (making sure to escape it if it's used in the expression) with / and # being the most common.

/^[A-Z]'?[- a-zA-Z]( [a-zA-Z])*$/
By the way your expression seems a bit too loose: it'll accept names like A' and A-.
Link to comment
Share on other sites

  • 1 month later...

You also don't need to capture the last part of the match.

A starting point to modify the regex would be

elseif( ! preg_match("~^[A-Z]'?[- a-zA-Z](?: [a-zA-Z])*$~", $_POST['lastNameForm']) )

but as requinix said, the match criteria are way loose.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.