Whenever you’re dealing with online forms it’s important to perform proper validation on the data that is submitted. Form validation ensures that the user will enter valid data on the form – minimizing your chances at possible SQL injections. PHP lets us perform server-side validation, and lets us display the appropriate error messages if a user enters incorrect values. Throughout this PHP tutorial we will discuss how to setup some basic required fields, and validate the data that get’s submitted through them.
Creating Required Fields with PHP
Required fields are the mandatory fields that must be filled out with our form. If the user does not enter a value in a required field, we have to show an error message to the user.
The following code example lets you make a text field a required field. The error message is stored in the variable ‘$err_address‘. The ‘empty()’ function checks if the input field is empty. The statement ‘<?php echo $err_address; ?>‘ displays the error message on the form.
If the user does not enter any value in the text field, the error message ‘Please enter your address. This field is mandatory.’ will be displayed, as shown in the screenshot below.
Email Validation With PHP
You can check whether an email address is valid or not using the ‘filter_var()‘ function. In the code below, we have passed two argument values to this function. The first argument value is our email address while the second one is a predefined constant.
The predefined constant ‘FILTER_VALIDATE_EMAIL‘ indicates that the ‘filter_var()‘ function will validate an email address.
URL Validation With PHP
The ‘filter_var()‘ function also allows you to check if a web URL is valid. In the code below, we have passed two argument values to this function. The first argument value is our website URL while the second one is a predefined constant.
The predefined constant ‘FILTER_VALIDATE_URL‘ indicates that the filter_var() function will validate a web URL. In the above code example, the URL is valid only if it is entered in this format: ‘https://www.mysite.com‘.
Name Validation With PHP
To validate a name, we have used this regular expression: ‘/^[a-zA-Z ]*$/‘ in the code below. The ‘preg_match()‘ function checks if the pattern specified in the regular expression exists in the second argument value, which is a string value. So in the code example below, this function checks if the specified pattern exists in the name.
In the example above, the entered name is valid if it contains only letters and whitespaces.
Password Validation With PHP
For password validation, we have used this regular expression: ‘/^.*(?=.{6,})(?=.*[0-9])(?=.*[A-Z]).*$/‘ in the code below, which specifies a particular password format. Use a different regular expression for a different password format.
In the example above, the password is valid if it is at least 6 characters long and if it contains at least one capital letter and at least one number.
Phone Number Validation With PHP
The code below validates a phone number. If the user enters the phone number in an invalid format, an error message is shown.
In the example above, a phone number is valid only if it is 10 digits long.
Postal Code/Zip Code Validation
The code below validates a postal code. If the user enters the postal code in an invalid format, an error message is shown. In the following example, a postal code is valid only if it is 6 digits long.
In this tutorial, you learned how to validate form data using PHP. Hopefully you’ve enjoyed this PHP tutorial. Feel free to leave any questions you might have in the comments section below.