Validate form fields Script

I wrote this class to get an easy to use form field validation script. Use this class to validate your database inputs or mail forms. Of course there is much more to validate: postcodes, special strings, credit card number etc. In this version is it possible to validate simple text, numbers (with decimals, too), dates, URLs, e-mail addresses and for HTML tags. Invalid form fields will be reported inside a detailed error message. If all form fields are valid you can use the boolean to submit your form. A new function in version 1.34 is the validation of check boxes and radio groups.

Visit here Script DemoPHP Script Download

Form field validation types

  • Text and strings (default)
  • Numbers and number with decimals
  • Date (US and European notation)
  • E-mail (
  • URL (
  • Checkboxes (and radio elements)


There are lot of classes, functions and scripts on the internet. Some of them are really hard to use. I tried with this class to make it so simple as possible.


You have to validate two text fields, one with the name and one with the e-mail address. The e-mail address is required and you have room for 30 characters in the text field.


$example = new Validate_fields; // create a new instance of the class
$example->add_text_field("name", "Peter Morris", "text", "n", 30); // define value for the name field
$example->add_link_field("email", "", "email", "y"); // and for the email field
$example->validation(); // call the validation method
$example->create_msg(); // this will show you the errors

Note: Use the values from your field variables ($_POST or $_GET) like:

$example->add_text_field("Name", $_POST['compl_name'], "text", "n", 30);

and use regular names (according the rules for variable names) for optimal screen messages (f. e. Name or Email, the same like the field label).


  • $fields (array) This array holds all your form information.
  • $messages (array) The class will store error information in this array.
  • $check_4html (boolean) Use this boolean if you want check all fields for HTML tags.
  • $language (string) Change this variable to use an other language then English for all messages.


  • Validate_fields() This is the constructor (at the moment with not so much function)
  • validation() The method which will do the whole validation
  • Use these methods to add a different kind of fields to the object.
    • add_text_field($name, $val, $type = “text”, $required = “y”, $length = 0)
    • add_num_field($name, $val, $type = “number”, $required = “y”, $decimals = 0, $length = 0)
    • add_link_field($name, $val, $type = “email”, $required = “y”)
    • add_date_field($name, $val, $type = “date”, $version = “us”, $required = “y”)
    • add_check_box($name, $element_name, $type = “checkbox”, $required_value = “”)
  • The attributes used for these methods are:
    • $name (string) = field name for the messages (all)
    • $val (string) = form field value (all)
    • $type (string) = validation type, see also at the begin of the document (all)
    • $required (string) = the information if a field is required or not, default = y (all)
    • $decimals (float) = how many decimals after the dot (default = 2) (number and decimal)
    • $version = (string) = use value to do a different date check (date only)
    • $length (integer) = The length to validate of a text or number field, use a “0” to disable this check (text and number)
    • $element_name = The name of a checkbox or radio element (checkbox only)
    • $required_value = The value needed to validate inside a radio group (checkbox only)
  • check_url($url_val, $field, $req = “y”) This method is used for the URL check
  • check_text($text_val, $field, $text_len = 0, $req = “y”) This method checks the text if required or about a valid string length
  • check_num_val($num_val, $field, $num_len = 0, $req = “n”) Number checking, validation of number of digits, if required and for numbers only.
  • check_decimal($dec_val, $field, $decimals = 2, $req = “n”) Checks a valid number with a given number of decimals.
  • check_date($date, $field, $req = “n”) The method validates a US-styled date notation and if a date is required or not.
  • check_email($mail_address, $field, $req = “y”) E-mail validation and checking if required or not.
  • check_check_box($req_value, $field, $element) Checkbox and radio element validation
  • check_html_tags($value, $field) If the variable $check_4html is set to true, all fields are check for the existens of HTML tags.
  • create_msg() This method is used to create a string from the messages array.
  • error_text($num, $fieldname = “”) This method is a library of messages. Add your own translations here. The variable $fieldname is needed to identify for which field the error exist.

Change log for the Validate form fields Script

Version 1.35

In this version the date validation is more flexible to accept also day and month numbers without a leading zero. While using the PHP function “checkdate()” there is a additional real date check. Several translations are added now: polish, Portuguese and Czech. Thank you all for the translations. And at last there was a small bug inside the create_msg method, I used in this version a different function to sort the array.

Version 1.34

A small improvement in the method create_msg() makes it possible to switch between the XHTML version and the simple html version. In the fields array one key was named “name”, it’s renamed to “value” to make it more clear. I removed the variable declarations at the begin of the method validation (thank you Mike Carter for pointing me on this). Because the value of a checkbox (radio) type field is only available if the element is checked there are new functions to validate this elements.

Version 1.33

The regular expression pattern of the check for html code wasn’t good, It’s working now. The example page is modified to test general text inside a textarea (useful to test the html code check). There are also Spanish translations for the messages now, thank you Mark for doing this job.

Version 1.32

There was a (last) small bug inside the email pattern, it’s fixed now. The Danish translations are added in this version too, thanks to Niels Fanáe for the translation and the help with the e-mail validation.

Version 1.31

The validation of e-mail addresses was not good. I changed the regex pattern to validate the most common cases.

Version 1.30

I optimized / modified the method for the URL check. Now is it possible to validate (nearly) all kind of URLs from the HTTP protocol.

Version 1.20

I added a new validation method: check_html_tags(), if the var $check_4html is set to true all fields will be validated for HTML tags. Negative integers will be validated too.

Version 1.11

Now is it possible to check European and US formatted dates just use the extra version parameter.

Version 1.10

I’ve split the method add_field() into three methods: add_text_field(), add_num_field(), add_link_field() and add_date_field() to make more clear which properties are needed.

Version 1.05

New method for URL validation, it’s called check_url(). Now you can validate URLs like http:/ The method create_msg() is called now inside the constructor.

Version 1.04

Added the Dutch translations to the error_text() method and also the German translations, thanks Theo good job!

Version 1.02

Added the method check_decimal() to the class. This method checks for values like: -0.50 or 123.567 The method’s validation() and add_field() are changed, too. (to take care of the new method)

Version 1.01

Changed the method error_text($num, $fieldname = “”) a little, to get a string with spaces in place of an underscore.