Bill
Bill

Reputation: 141

Trying to validate a name field to be sure it is all alpha characters or a hyphen or apostrophe

if(!preg_match("/[a-zA-Z'-]/",$First)) { die ("invalid first name");}

the above only flags input as invalid when the field is all numeric. combinations of letters and numbers pass ok. Some help here for a newby please. thanks.

Upvotes: 11

Views: 40050

Answers (7)

cjohansson
cjohansson

Reputation: 1093

If you use if (!preg_match("/^[a-zA-Z'-]+$/", $firstName)) { it will also match if an error occured, like a invalid regexp, because the ! matches for both the return values FALSE and 0.

From the PHP Manual: preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.

So the best way of making sure that the name is valid by your requirements is:

if (preg_match("/^[a-zA-Z'-]+$/", $firstName) === 1) {
    die('Valid name');
} else {
    die('Invalid name or error');
}

Upvotes: 0

Gozie
Gozie

Reputation: 31

Here's an solution I was able to come up with. Should be able to handle your validation issues for names that have apostrophes, hypens and spaces.

	if(!preg_match('/^([a-zA-Z]+[\'-]?[a-zA-Z]+[ ]?)+$/', $string))

Upvotes: 0

Jeremy Knight
Jeremy Knight

Reputation: 604

Adding accented characters is probably smart too, so that "Pierre Bézier" can fill out your form. Adding:

À-ÿ

.. to your regex will do that. Something like this, with everything included:

/^([a-zA-ZÀ-ÿ-' ]+)$/

Upvotes: 2

STEEL
STEEL

Reputation: 10057

You can try this one, incase you want FULL NAME e.g. "JOHN RAMBO"

While the HTML FORM.PHP

<div class="error"><?php echo $error['name']; ?></div>
<input name="name" type="text" value="<?php echo $input['name']; ?>" class="inputf">

PHP CONTACT PAGE

if(!preg_match("/^[a-zA-Z'-]/", $input['name']))
{
    // name is in-valid
    $error['name'] = 'Invalid Name';
    include('views/form.php');
}

Upvotes: 1

John Knoeller
John Knoeller

Reputation: 34148

In this case better to look for invalid characters than to try and match all of the characters, once a single invalid character appears, the search can quit and return failure. This is more efficient than always scanning the whole string.

if (preg_match("/[^A-Za-z'-]/", $First)) { die ("invalid first name"); }

the ^ inside the set [] makes it match everything not in the set. and since the string is invalid if we have even one invalid character, there is no need for the set to have a repetition operator.

Even better would be a more helpful error message

if (preg_match("/[^A-Za-z'-]/", $First, $Inv)) { die ("{$Inv[0]} not allowed in first name"); }

Upvotes: 3

cletus
cletus

Reputation: 625087

You're only matching a single character. You need to add a wildcard like + (1 or more). Also you're matching anywhere in the string. You need to add anchors (^ for start of string, $ for end of string) to make sure there are no invalid characters.

if (!preg_match("/^[a-zA-Z'-]+$/", $First)) { die ("invalid first name");}

Alternatively you can look for a non-matching character:

if (preg_match("/[^a-zA-Z'-]/", $First)) { die ("invalid first name");}

The ^ inside the square brackets here means "any character that is NOT one of these".

Upvotes: 4

Andy E
Andy E

Reputation: 344575

Try:

if(!preg_match("/^[a-zA-Z'-]+$/",$First)) { die ("invalid first name");} 

The ^ matches the beginning of the string, the $ matches the end of the string and + after a character group means "one or more" matching characters.

Upvotes: 19

Related Questions