mkValidator v2.0 – PHP Validation Script

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. 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.

I wrote a validation script and it’s been implemented in many projects. I have update my script to version 2.0 by adding some new validation and uses PHP filter to validate which is faster than before.

Here is the code,

 * mkValidator v2.0
 * @license    GPL 2 (
 * @author     Muthu Kumaran <>
 * @website

 * Note: Filter extension is required in PHP which comes default enabled from 5.2.0. If not please enable the Filter Support.
 * About Filter

 * argument explaination,
 * mkValid(
            $value  - input values
            $type   - validation type
            $length - [optional] [default 1] to set minimum string limit

function mkValid($value,$type,$length=1){
        case 'req':     //check if required and with string length
            if(strlen(trim($value)) < $length) return false;
        case 'trim':    //check if empty
            if(!trim($value)) return false;
        case 'char':    //only character and space are allowed
            if(!preg_match('/^[a-zA-Z ]*$/',$value)) return false;
        case 'int':     //check integer
            if(!filter_var($value, FILTER_VALIDATE_INT)) return false;
        case 'float':   //check float
            if(!filter_var($value, FILTER_VALIDATE_FLOAT)) return false;
        case 'email':   //check email format
            if(!filter_var($value, FILTER_VALIDATE_EMAIL)) return false;
        case 'phone':   //check phone format
            if(!preg_match('/^([0-9]{3})([0-9]{3})([0-9]{4})$/', $value)) return false;
        case 'code':    //check username or code format
            if(!preg_match('/^[a-z0-9_.]*$/', $value)) return false; #not tested
        case 'zip-us':  //check US-Zip format
            if(!preg_match('/^[0-9]{5}$/', $value)) return false;
        case 'zip-ca':  //check CANADA-Zip format
            if(!preg_match('/^[a-z]{1}[0-9]{1}[a-z]{1}[0-9]{1}[a-z]{1}[0-9]{1}$/i', $value)) return false;
        case 'url':     //check url format
            if(!filter_var($value, FILTER_VALIDATE_URL)) return false;
        case 'ip':      //check IP address
            if(!filter_var($value, FILTER_VALIDATE_IP)) return false;
    return true;


Here is some simple example and the usage of mkValidtor script.



echo mkValid(' ','req');        //return false
echo mkValid('Muthu','req',3);  //return true

#Check char with space
echo mkValid('Will Smith','char');      //return true

echo mkValid('','email'); //return true

#Check URL
echo mkValid('','url'); //return true

echo mkValid('3','int');                //return true

echo mkValid('3.00','float');           //return true

#To check IP address
echo mkValid('','ip');   //return false
echo mkValid('','ip');   //return true


How to use it in the projects,


//Validate Name
if(!mkValid($_POST['name'],'req') && !mkValid($_POST['name'],'char')){
    echo 'This field is requied';

//validate Email
    echo 'Please enter the email';
    echo 'Please enter a valid email';

//validate Age
    echo 'Please enter a valid number for age';

//validate username
    echo 'Please enter username';
    echo 'Please enter a valid username';


mkValidator v2.0 requires Filter extension which comes default enabled from PHP 5.2.0

Download mkValidator v2.0

If you are using PHP 5.1.x or older, then you can use mkValidator v1.1.

Suggestions are welcome to improve the code.

  1. Sivaraj

    Good one. Thanks for releasing the validation script.
    Expecting more LAMP related articles in your website.

  2. Katlyn Robbins

    Keep on working, great job!

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>