skip to content »

Validating forms using php

We have a registration form containing name, password, email and gender fields. PHP provides empty() function to check a variable is empty.

validating forms using php-46validating forms using php-2validating forms using php-61

Be aware of that any Java Script code can be added inside the - this would not be executed, because it would be saved as HTML escaped code, like this: <script>location.href(' The code is now safe to be displayed on a page or inside an e-mail.Moreover, since you are doing the same or similar validation for most of the forms that you make, just too much of duplicate effort is spent on form validations.Note: You can make web forms quickly with Simfatic Forms. Note that it is not a string, you don't have to use quotes and it won't work if you use quotes.Let's chop the regular expression notation so we can understand it better : So in english the search replace function above can be read as : "Replace one or more whitespace character from the beginning or ending of a string with blank character" As for the email input, we need to double check it. A valid email format can be described as : [ a string consisting of alphanumeric characters, underscores, dots or dash ] @ ( [ a valid domain name ] DOT [ a valid TLD ]) OR [a valid IP adress ] In case you're wondering TLD means Top Level Domain such as com, net, org, biz, etc.It is very essential to have the input to your form validated before taking the form submission data for further processing.

When there are many fields in the form, the PHP validation script becomes too complex.

The script works fine even if the user does not enter any data.

The next step is to make input fields required and create error messages if needed. Thanks for this nice article.

While Validating an HTML form the client side validation is not only enough.

The HTML form we will be working at in these chapters, contains various input fields: required and optional text fields, radio buttons, and a submit button: The validation rules for the form above are as follows: This code adds a script tag and an alert command.