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,

<?php
/**
 * mkValidator v2.0
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Muthu Kumaran <emailiamkumaran@gmail.com>
 * @website    http://www.iamkumaran.com/

 * Note: Filter extension is required in PHP which comes default enabled from 5.2.0. If not please enable the Filter Support.
 * About Filter http://www.php.net/manual/en/book.filter.php

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

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

 

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

Example:

<?php

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

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

#Email
echo mkValid('Will.Smith@email.co.in','email'); //return true

#Check URL
echo mkValid('http://www.google.com/123','url'); //return true

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

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

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

?>

How to use it in the projects,

<?php

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

//validate Email
if(!mkValid($_POST['email'],'req')){
    echo 'Please enter the email';
}elseif(!mkValid($_POST['email'],'email')){
    echo 'Please enter a valid email';
}

//validate Age
if(!mkValid($_POST['age'],'int')){
    echo 'Please enter a valid number for age';
}

//validate username
if(!mkValid($_POST['username'],'req')){
    echo 'Please enter username';
}elseif(!mkValid($_POST['username'],'code')){
    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.

Pages:
  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>