|
Za sve kojima treba neka prosta validacija a nece da je traze po netu, mogu koristiti ovu klasu...
Dovoljno je samo instancirati objekat i pozvati metodu check.
Metoda check prima 2 parametra, prvi je string koji zelite da proverite, a drugi moze biti ili neka od rezervisanih reci za koje je vec napisan regularni izraz, ili sam regularni izraz..
<?php
/**
* Copyright (C) 2008 Boban Karišik <karisikb@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Validate {
var $email = '/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/s';
var $phone = '/[0-9\-\/]{6,15}\s/';
var $url = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/s';
var $username = '/^[a-z\d_]{5,20}$/i';
var $image = '/(\.gif|\.jpg|\.png)$/i';
var $ip = '/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/s';
var $hexColor = '/#([a-fA-F0-9]{3})|([a-fA-F0-9]{6})$/';
var $date = '/\d{1,2}(\.|\/)\d{1,2}(\.|\/)(\d{2}|\d{4})(\.|$)$/';
var $empty = '/.+/';
var $regEx;
/**
* Check an string for regular expression matches
*
* @param string $string - String you want to check
* @param string $exp - Regular expression or reservated word (email, phone, url, username, image, ip, hexCode, date, empty)
*/
function check($string, $exp = 'empty') {
$this->regEx = ($this->$exp != '') ? $this->$exp : $exp;
return preg_match($this->regEx, $string);
}
}
?>
Primer
$val = new Validate();
if($val->check('some@email.net','email')) {
echo 'true' ;
} else {
echo 'false';
}
p0z
|