0

I am trying to validate form input data using PHP's preg_match function. I am a little confused of how to use it. If I want to validate say an alphanumeric string, I would use ^[0-9a-zA-Z ]+$ as the first parameter and the string we're validating as the second one. But how would I use preg_match to tell if it's valid or not? Would I do this:

if(preg_match("^[0-9a-zA-Z ]+$", $_POST['display_name'])){
  "String is valid";
} else {
  "String is not valid";
}

Or the other way around? I am currently using the if not preg_match if statement but it's returning false for some reason... I know this is probably an easy answer, but I cannot figure this out.

5
  • Why don't you just use ctype_alnum()? Commented Oct 18, 2011 at 3:15
  • @Johnsyweb, ctype_alnum() does not include spaces. Commented Oct 18, 2011 at 3:19
  • @Radu: I wasn't sure whether the space was a typo. The OP stated that they wanted "to validate say an alphanumeric string". Commented Oct 18, 2011 at 3:21
  • I think what you need to do first is to pick best answers on those 12 other questions without best answers. Commented Oct 18, 2011 at 3:28
  • it's returning false for some reason... You would have gotten a helpful warning, if you hadn't turned down error_reporting() Commented Oct 18, 2011 at 4:08

2 Answers 2

3

FALSE return from a preg_match indicates an error

you need to delimit your regex (see the leading and trailing / you can use other characters too

if (preg_match("/^[0-9a-zA-Z ]+$/", $_POST['display_name'])) {
Sign up to request clarification or add additional context in comments.

5 Comments

escape? you mean add a delimiter
Just some nitpicking: a regex cannot return false, only a function like preg_match that uses a regex can. It also does't necessarily indicate an error, if preg_match returns false it means there was no match. preg_match does throw an error if the regex is invalid, but apparently the OP has turned off error reporting. :)
@deceze i have edited the returns false from regex to returns false from preg_match. according to the manual it says returns false on error, returns 0 for no match. although i have not run tests to verify that the manual is correct.
Oh, there you go. It slipped my mind that preg_match usually returns 0 or 1. So ignore that point of my picking. :)
Not quite true... Yes, the PHP documentation for preg_match() says that it returns FALSE on error. Unfortunately, PHP versions 5.3.3 and below have a bug (#52732) where preg_match() does NOT return FALSE on error (it instead returns int(0), which is the same value returned in the case of a non-match). This bug was fixed in PHP version 5.3.4.
1

You need add the delimiters of your pattern, like this:

preg_match("/^[0-9a-zA-Z ]+$/", $_POST['display_name'])

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.