TMNuclear
TMNuclear

Reputation: 1175

php preg match a-zA-Z and only one space between 2 or more words

For my PHP script I have this code:

if (!preg_match("/[^A-Za-z]/", $usersurname))
   $usersurname_valid = 1; 

This worked untill I realized a surname can be two or more words... doh.

Anyone can tell me how to write this code if I want to allow 1 space between two worlds? For example:

Jan Klaas is now wrong and Jan Klaas should be allowed, also Jan Klaas Martijn and so on should be allowed.

Even better would be a preg replace, to replace two or more spaces with 1, so when you write:
Jan(space)(space)Klaas or Jan(space)(space)(space)(space)Klaas, it would return Jan(space)Klaas.

I searched around for a while but somehow I just can't get this space matching to work..

PS: When I got this working, I will apply this for the mid and last name too ofcourse.

===========================================

EDIT: After you helping me out, I re-wrote my code to:

// validate usersurname
$usersurname = preg_replace("/\s{2,}/"," ", $usersurname);
if (!preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/",$usersurname))
    $usersurname_valid = 1; 
// validate usermidname
$usermidname = preg_replace("/\s{2,}/"," ", $usermidname);
if (!preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/",$usermidname))
    $usermidname_valid = 1;
// validate userforename
$userforename = preg_replace("/\s{2,}/"," ", $userforename);
if (!preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/",$userforename))
    $userforename_valid = 1;

and the error notifications

elseif ($usersurname_valid !=1)
    echo ("<p id='notification'>Only alphabetic character are allowed for the last name. $usersurname $usermidname $userforename</p>");
// usermidname character validation
elseif ($usermidname_valid !=1)
    echo ("<p id='notification'>Only alphabetic character are allowed for the middle name. $usersurname $usermidname $userforename</p>");
// userforename character validation
elseif ($userforename_valid !=1)
    echo ("<p id='notification'>Only alphabetic character are allowed for the (EDIT) first name. $usersurname $usermidname $userforename</p>");

Replacing the spaces are working well and I need this preg_match to check on on A-Za-z + space. I think in this case it doesn't matter if it's matching more than 1 spaces because it's replaced anyway, right?

EDIT:

Solution for my case:

$usersurname = preg_replace("/\s{2,}/"," ", $usersurname);
if (!preg_match("/[^A-Za-z ]/", $usersurname))

This does the work. Thanks for helping out, J0HN

Upvotes: 1

Views: 6617

Answers (1)

J0HN
J0HN

Reputation: 26961

Well, solving the problem you have in mind:

if (!preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/",$usersurname)) { ... }

But, well, it's just a part of the solution, and it's not bulletproof. Look at the list of common mistakes when handling names.

So, you'd better to re-think on your validation approach.

Replacing the multiple spaces is simpler to achieve as a separate instruction, something like

$processed_usersurname = preg_replace("/\s{2,}/"," ", $usersurname); 

This will match and replace any two or more consequent whitespace characters (space, tab, linebreak and carriage return) to single space

Upvotes: 3

Related Questions