Home / Programming / PHP / 14. PHP Forms – Validation With Required Fields

14. PHP Forms – Validation With Required Fields


Hi everyone, this time we will discuss about PHP Validation using the Required Field. Not many people know, it turns out that the application of required fields in PHP is very easy and practical. Without waiting too long, this section tells the best way to make input fields required and make blunder messages if necessary. Please listen and don’t forget to put it into practice on your application. PHP Forms – Validation With Required Fields.

PHP – Required Fields

From the approval rules table on the past page, we see that the “Name”, “Email”, and “Gender” fields are required. These fields can’t be vacant and must be rounded out in the HTML structure.

FieldValidation Rules
NameRequired. + Must only contain letters and whitespace
E-mailRequired. + Must contain a valid email address (with @ and .)
WebsiteOptional. If present, it must contain a valid URL
CommentOptional. Multi-line input field (textarea)
GenderRequired. Must select one

PHP Forms – Validation With Required Fields

PHP Forms - Validation With Required Fields
PHP Forms – Validation With Required Fields

In the accompanying code we have included some new factors: $nameErr, $emailErr, $genderErr, and $websiteErr. These mistake factors will hold blunder messages for the necessary fields. We have additionally included an if else articulation for each $_POST variable. This checks if the $_POST variable is vacant (with the PHP unfilled() work). On the off chance that it is vacant, a blunder message is put away in the distinctive mistake factors, and in the event that it isn’t unfilled, it sends the client input information through the test_input() work:

PHP – Display The Error Messages

At that point in the HTML structure, we include a little content after each necessary field, which creates the right blunder message if necessary (that is if the client attempts to present the structure without rounding out the necessary fields):

The following stage is to approve the info information, that is “Does the Name field contain just letters and whitespace?”, and “Does the E-mail field contain a substantial email address language structure?”, and whenever rounded out, “Does the Website field contain a legitimate URL?”.
  • Facebook
  • Twitter
  • Linkedin
  • Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *

This div height required for enabling the sticky sidebar
Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :