Monday, May 28, 2007

PHP Server Side Validation


I have made a PHP Server Side Validator for PHP4 and PHP5. It is a simple server side validator, which is based on OOPs concept. Those who want to save time while developing any PHP application & wants server side validation of their data on the fly, can use this class.

Use PHP validator class (validation.class.php), follow some simple rules & validate your data.

How to use validation class?

# Include validation class
require_once("validation.class.php")

# Create Object of class validation
$obj = new validation();

# Call add_fields() functions for all the fields, for which you want server side validation
$obj->add_fields($postVar, $authType, $errorMsg);

# Validate your data using validate() function, which will return errors if any...
$error = $obj->validate();

if($error){
# Display ERROR messages...
}else{
# Proceed with other code...
}

How to use add_fields($postVar, $authType, $errorMsg) function?

$postVar: $_POST, $_GET, $_FILES e.g.$_POST['username'], $_FILES['images']
$authType: Authentication/validation Type
(Currently supports 26 validation types as follow)

(01) req - Check for blank, unselected, unchecked field/fields

(02) alpha - Only alphabets are allowed (az, AZ)

(03) alphanum - Only alphanumerics are allowed (az, AZ, 09)

(04) num - Only numbers are allowed (09)

(05) max - Check for maximum character length e.g. max=10
i.e. only a string of maximum length 10 is allowed

(06) min - Check for minimum character length e.g. min=6
i.e. only a string of minimum length 6 is allowed

(07) lte - value should be less than/equal to some value. e.g lte=2
In case of Selection Box, Check Box it checks for minimum no of selections.

(08) gte - value should be greater than/equal to some value. e.g gte=4
In case of Selection Box, Check Box it checks for maximum no of selections.

(09) username - validate username

(10) name - validate first name, last name

(11) address - validate address

(12) phone - validate phone no.Allowed Options:
in-India, us-US, fr-France, sw-Swedish, br-Brazil

(13) mobile - validate Mobile no.

(14) zip - validate ZIP code

(15) uszip - validate US ZIP code

(16) ukzip - validate UK ZIP code

(17) ssn - validate Social Security Number

(18) currency - validate currency

(19) email - validate EMAIL address

(20) url - validate URL

(21) ip - validate IP address

(22) date - validate date default: ddmmyyyy e.g. date=ddmmyyyy,dd/mm/yyyy
Allowed Date Formats:
# ddmmyyyy, ddmmyy, dd/mm/yyyy, dd/mm/yy, dd.mm.yyyy, dd.mm.yy
# mmddyyyy, mmddyy, mm/dd/yyyy, mm/dd/yy, mm.dd.yyyy, mm.dd.yy
# yyyyddmm, yyddmm, yyyy/dd/mm, yy/dd/mm, yyyy.dd.mm, yy.dd.mm
# yyyymmdd, yymmdd, yyyy/mm/dd, yy/mm/dd, yyyy.mm.dd, yy.mm.dd

(23) ftype - validate file types e.g. ftype=jpg,gif,png
Allowed Options:
txt, xml, csv
zip, tar, ctar
pdf, doc, xls, ppt
jpg, gif, bmp, icon, font

(24) fsize - validate file size e.g. fsize=500

(25) imgwh - validate image width, heigth e.g. imgwh=800,600
You will need to compile PHP with the GD library of image functions for this to work.

(26) custom - custom validation e.g. custom=/^some regular exp$/

$errorMsg: Error message to be displayed upon error

Download the source code from
http://sourceforge.net/projects/php-validator/

See Demo at
http://php-validator.sourceforge.net/

Feel free to comment! :)

4 comments:

  1. Great work....!!!!!

    but i m little confused with class code...

    you should use comments to differentiate them...

    Keep Going

    ReplyDelete
  2. good word keep it up

    ReplyDelete
  3. Thats fantastic

    ReplyDelete
  4. Thanks for such a great help and your helpful nature.

    ReplyDelete