PHP Classes
elePHPant
Icontem

File: my_validation.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Gianluca Zanferrari  >  My Validation  >  my_validation.php  >  Download  
File: my_validation.php
Role: Example script
Content type: text/plain
Description: usage example
Class: My Validation
Validate request values according to custom rules
Author: By
Last change:
Date: 1 year ago
Size: 4,123 bytes
 

Contents

Class file image Download
<?php session_start();
setcookie('n', '10-10-2013', time()+3600);
require_once(
'my_validation.class.php');

$chk = new my_validation;

$_POST['n'] = 1;
echo (
$chk->validate('n', '= 1', 'post')) ? '$_POST[\'n\'] = 1 -> OK' : '$_POST[\'n\'] = 1 -> ERROR';
echo(
'<br>');
$_GET['n'] = 1;
echo (
$chk->validate('n', '= 1', 'get')) ? '$_GET[\'n\'] -> OK' : '$_GET[\'n\'] -> ERROR';
echo(
'<br>');
$_SESSION['n'] = '10-10-2013';
echo (
$chk->validate('n', 'date', 'session')) ? '$_SESSION[\'n\'] date -> OK' : '$_SESSION[\'n\'] date -> ERROR';
echo(
'<br>');
echo (
$chk->validate('n', 'date' ,'cookie')) ? '$_COOKIE[\'n\'] date -> OK' : '$_COOKIE[\'n\'] date -> ERROR';
echo(
'<br>');
$n = NULL;
echo (
$chk->validate('n')) ? 'NULL -> OK' : 'NULL -> ERROR';
echo(
'<br>');
$n = 1;
echo (
$chk->validate('n')) ? $n.' -> OK' : $n.' -> ERROR';
echo(
'<br>');
$n = 0;
echo (
$chk->validate('n')) ? $n.' -> OK' : $n.' -> ERROR';
echo(
'<br>');
$n = 1;
echo (
$chk->validate('n', '== 1')) ? '1 == 1 -> OK' : '1 == 1 -> ERROR';
echo(
'<br>');
$n = 1;
echo (
$chk->validate('n', '> 2')) ? '1 > 2 -> OK' : '1 > 2 -> ERROR';
echo(
'<br>');
$n = 1;
echo (
$chk->validate('n', '!= 2')) ? '1 != 2 -> OK' : '1 != 2 -> ERROR';
echo(
'<br>');
$n = 1;
echo (
$chk->validate('n', '<> 2')) ? '1 <> 2 -> OK' : '1 <> 2 -> ERROR';
echo(
'<br>');
$n = 1;
echo (
$chk->validate('n', '<> 1')) ? '1 <> 1 -> OK' : '1 <> 1 -> ERROR';
echo(
'<br>');
$n = 1;
echo (
$chk->validate('n', '>= 1')) ? '1 >= 1 -> OK' : '1 >= 1 -> ERROR';
echo(
'<br>');
$n = 1;
echo (
$chk->validate('n', '>= 2')) ? '1 >= 2 -> OK' : '1 >= 2 -> ERROR';
echo(
'<br>');
$n = 2;
echo (
$chk->validate('n', 'between 1,3')) ? '2 between 1 and 3 -> OK' : '2 between 1 and 3 -> ERROR';
echo(
'<br>');
$n = 2;
$arr = array(100,2,300,150,1000);
$values = implode(',',$arr);
echo (
$chk->validate('n', 'in '.$values)) ? '2 in '.$values.' -> OK' : '2 in '.$values.' -> ERROR';
echo(
'<br>');
$n = 1;
echo (
$chk->validate('n', 'float')) ? '1 float -> OK' : '1 float -> ERROR';
echo(
'<br>');
$n = 1.554226;
echo (
$chk->validate('n', 'float')) ? '1.554226 float -> OK' : '1.554226 float -> ERROR';
echo(
'<br>');
$n = 1;
echo (
$chk->validate('n', '!empty')) ? '1 !empty -> OK' : '1 !empty -> ERROR';
echo(
'<br>');
$n = 0;
echo (
$chk->validate('n', '!empty')) ? '0 !empty -> OK' : '0 !empty -> ERROR';
echo(
'<br>');
$n = 1;
echo (
$chk->validate('n', 'alnum')) ? '1 alnum -> OK' : '1 alnum -> ERROR';
echo(
'<br>');
$n = '1ff';
echo (
$chk->validate('n', 'alnum')) ? '1ff alnum -> OK' : '1ff alnum -> ERROR';
echo(
'<br>');
$n = '1ff*';
echo (
$chk->validate('n', 'alnum')) ? '1ff* alnum -> OK' : '1ff* alnum -> ERROR';
echo(
'<br>');
$n = 'ff';
echo (
$chk->validate('n', 'alfa')) ? 'ff alfa -> OK' : 'ff alfa -> ERROR';
echo(
'<br>');
$n = '1ff';
echo (
$chk->validate('n', 'alfa')) ? '1ff alfa -> OK' : '1ff alfa -> ERROR';
echo(
'<br>');
$n = 0;
echo (
$chk->validate('n', 'digit')) ? '0 digit -> OK' : '0 digit -> ERROR';
echo(
'<br>');
$n = '1ff';
echo (
$chk->validate('n', 'digit')) ? '1ff digit -> OK' : '1ff digit -> ERROR';
echo(
'<br>');
$n = '10-10-2013';
echo (
$chk->validate('n', 'regex /^(0[1-9]|[12][0-9]|3[01])[- \.](0[1-9]|1[012])[- \.](19|20)\d\d$/')) ? '10-10-2013 regex -> OK' : '10-10-2013 regex -> ERROR';
echo(
'<br>');
$n = '10-1-2013';
echo (
$chk->validate('n', 'regex /^(0[1-9]|[12][0-9]|3[01])[- \.](0[1-9]|1[012])[- \.](19|20)\d\d$/')) ? '10-1-2013 regex -> OK' : '10-1-2013 regex -> ERROR';
echo(
'<br>');
$n = array(1);
echo (
$chk->validate('n', 'array')) ? 'array(1) array -> OK' : 'array(1) array -> ERROR';
echo(
'<br>');
$n = 'info@me.com';
echo (
$chk->validate('n', 'email')) ? 'info@me.com email -> OK' : 'info@me.com email -> ERROR';
echo(
'<br>');
$n = '10-10-2013';
echo (
$chk->validate('n', 'date')) ? '10-10-2013 date -> OK' : '10-10-2013 date -> ERROR';
echo(
'<br>');
$n = 'èeëe';
echo (
$chk->validate('n', 'latin')) ? 'èeëe latin -> OK' : 'èeëe latin -> ERROR';
echo(
'<br>');
echo (
$chk->validate('notExistent')) ? 'Not existent var -> OK' : 'Not existent var -> ERROR';
echo(
'<br>');
?>