I created 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 checkboxes and radio groups.
Visit here Script Demo - PHP Script Download
Documentation and support
All update information - Post here your questions and suggestions.
Form field validation types
- Text and strings (default)
- Numbers and number with decimals
- Date (US and European notation)
- E-mail ([email protected])
- URL (http://www.domain.com/page.html)
- Checkboxes (and radio elements)
How to use?
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 protected]", "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 fot 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 differnt 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) = Fieldname 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 (deafault = 2) (number and decimail)
- $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 agiven 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.