How can i validate a name in php?

Home  »  How To Guides  »  PHP   »   How to Validate First and Last Name using Regular Expression in PHP

When you allow the user to submit data on the server, it’s a good idea to validate data before further processing. A name field is a common form filed for a web application. Here we will show how you can validate the name filed to check whether the user provides their first and last name.

The Regular Expression (REGEX) is the easiest way to validate full name format in PHP. You can easily validate first and last name using regular expression in PHP. Use PHP preg_match() function to perform a regular expression match.

The following example code uses preg_match() function and simple REGEX for name validation in PHP.

preg_match("/^([a-zA-Z' ]+)$/","Given_Name");

Usage:

if(preg_match("/^([a-zA-Z' ]+)$/",$givenName)){
    echo 
'Valid name given.';
}else{
    echo 
'Invalid name given.';
}

I have been creating some validation for a signup form, and I have come across an error when validating the user's name.

I have used an input of "Test" for both firstname and lastname and I still get an error when it should allow the input, since !preg_match('/^[a-z]*$/i', $value) should allow both lower and uppercase characters?

Also the error that I get seems to be only triggering for the firstname and not last name.

 $value){
            if($field === 'firstname' || $field === 'lastname' && !preg_match('/^[a-z]*$/i', $value)){
                $errors[] = "{$field} has invalid characters please try again.";
                echo $error;
            }         
        }
}
?>

How can i validate a name in php?

asked Feb 9, 2018 at 17:33

3

You are missing brackets around your "OR" condition. The "AND" takes precedence over "OR".

if(($field === 'firstname' || $field === 'lastname') && !preg_match('/^[a-z]*$/i', $value)){
     $errors[] = "{$field} has invalid characters please try again.";
}

NB: In your code above, you have an "undefined variable" : $error.

answered Feb 9, 2018 at 17:37

How can i validate a name in php?

SyscallSyscall

18.3k10 gold badges33 silver badges49 bronze badges

0

I recommend that:

  1. you make your conditional more direct/brief/DRY with: in_array($key,['firstname','lastname']) instead of two separate checks.

  2. you avoid the unnecessary use of a regex function when there is a non-regex function that was designed to perform the same task: !ctype_alpha($value)

This is what the implementation could look like:(Demo)

$_POST=['submit'=>'','firstname'=>'legit','lastname'=>'n0tl3g!t'];

$errors=[];
foreach($_POST as $key=>$value){
    if(in_array($key,['firstname','lastname']) && !ctype_alpha($value)){
        $errors[]="$key field contains invalid value: $value";
    }
}
var_export($errors);

Output:

array (
  0 => 'lastname field contains invalid value: n0tl3g!t',
)

As for improving the UX of your application, I recommend using HTML5 validation in your form to prevent the submission of invalid field values like so...


answered Feb 14, 2018 at 2:35

How can i validate a name in php?

mickmackusamickmackusa

39k11 gold badges76 silver badges113 bronze badges

How do you validate user inputs in PHP?

PHP has filter_var() function to validate variables. We can set it's second parameter to different values and use it to validate emails, URLs, integers, booleans, etc. This function returns false on failure or invalid input. We can simply validate an email using filter_var() function and FILTER_VALIDATE_EMAIL flag.

Can validation be done in PHP?

Form Validation is a necessary process before the data entered in the form is submitted to the database. This is done to avoid unnecessary errors. In PHP Form validation, the script checks for data in respective fields based on the rules set by the developer, and returns an error if it does not meet the requirements.

What is validation rule in PHP?

There are two types of validation are available in PHP. They are as follows − Client-Side Validation − Validation is performed on the client machine web browsers. Server Side Validation − After submitted by data, The data has sent to a server and perform validation checks in server machine.

Which are the types of validation in PHP?

Client-Side Validation: This type of validation is done on the web browser of the client machine. Server-Side Validation: The data on submission is then sent to the server machine to perform validation checks there.