Methods for data validation combined in class Validate defined in the file includes/validate.php.
They can be used in models and handlers POST requests directly to verify the transmitted data.
string|boolean false login (string $name, [integer $id = 0]) – Проверяет логин на валидность.
Example:
$names = array('', 'about_butterflies_site_administrator', 'dbs ', 'admin', 'my_login');
foreach($names as $name)
{
echo '"'.$name.'": '.Validate::login($name);
}
// выведит:
// "": Enter login.
// "administraor_sajta_pro_babochek": Login exceeds max length: 15.
// "dbs ": Login cannot end with space.
// "admin": User with this login already exists.
// "my_login":
string|boolean false mail (string $mail) – Проверяет e-mail на валидность.
Example:
echo Validate::mail('myemailbez@');
// output: Incorrect E-mail.
if(! Validate::mail('my@site.com'))
{
echo 'Validation passed.';
}
else
{
echo 'Validation is not passed.';
}
// output: Validation passed.
string|boolean false mail_user (string $mail, [integer $id = 0]) – Проверяет зарегистрирован ли пользователь с указанным электронным ящиком.
Example:
$mes = Validate::mail_user('my@site.com');
if($mes)
{
echo $mes;
}
else
{
echo 'Validation passed.';
}
// output:
// User with this login already exists.
// or
// Validation passed.
string|boolean false phone (string $phone) – Проверяет телефон на валидность.
Example:
$phones = array('999-99-99', '+7 (999) 999-99-99', '999999999999');
foreach($phones as $phone)
{
echo $phone.': ';
$mes = Validate::phone($phone);
if($mes)
{
echo $mes;
}
else
{
echo 'Validation passed.';
}
}
// output:
// 999-99-99: Incorrect number.
// +7 (999) 999-99-99: Validation passed.
// 999999999999: Validation passed.
string|boolean false password (string $password, [boolean $is_simple = false]) – Проверяет пароль на валидность.
Example:
echo Validate::password('');
// output: Enter password.
echo Validate::password('123', true);
// output: The entered password is in TOP100 of the most cracked passwords. Think of another password.
string|boolean false numtext (string $value) – Проверяет число.
Example:
$ints = array('345', 23, '9a');
foreach($ints as $int)
{
echo $int.': ';
$mes = Validate::numtext($int);
if($mes)
{
echo $mes;
}
else
{
echo 'Validation passed.';
}
}
// output:
// 345: Validation passed.
// 23: Validation passed.
// 9a: Числовое значение должно содержать только цифры.
string|boolean false floattext (string $value) – Проверяет число с плавающей точкой.
Example:
$fls = array('345', 23, '9a', '13.50', '189,45', 3456.9);
foreach($fls as $f)
{
echo $f.': ';
$mes = Validate::floattext($f);
if($mes)
{
echo $mes;
}
else
{
echo 'Validation passed.';
}
}
// output:
// 345: Validation passed.
// 23: Validation passed.
// 9a: Numerical value must contain only numerals and point or comma.
// 13.50: Validation passed.
// 189,45: Validation passed.
// 3456.9: Validation passed.
string|boolean false date (string $value) – Проверяет дату.
Example:
$ds = array('13.05.2014', '40.05.2014', '13 мая');
foreach($ds as $d)
{
echo $f.': ';
$mes = Validate::date($d);
if($mes)
{
echo $mes;
}
else
{
echo 'Validation passed.';
}
}
// output:
// 13.05.2014: Validation passed.
// 40.05.2014: Day cannot be more than 31.
// 13 мая: Incorrect date. Reenter date like dd.mm.yyyy.
string|boolean false datetime (string $value) – Проверяет дату и время.
Example:
$ds = array('13.05.2014', '13.05.2014 23:30', '13.05.2014 24:30', '13 мая');
foreach($ds as $d)
{
echo $f.': ';
$mes = Validate::datetime($d);
if($mes)
{
echo $mes;
}
else
{
echo 'Validation passed.';
}
}
// output:
// 13.05.2014: Validation passed.
// 13.05.2014 23:30: Validation passed.
// 13.05.2014 24:30: Hour cannot be more than 23.
// 13 мая: Incorrect date and time. Reenter date like dd.mm.yyyy hh:mm.
string|boolean false text (string $text, [integer $max_lenght = 40]) – Проверяет текст на наличие длинных слов.
Example:
echo Validate::text('In this text, there is a verybigwordwhichcanspoilthelayoutofthesmallspeakers.');
// output: Error! Exceeded maximum length of one word!
boolean url (string $url, [boolean $absolute = false]) – Проверяет URL на валидность.
Example:
$urls = array('www.diafancms.com', 'http://www.diafancms.com/');
foreach($urls as $url)
{
echo $url.': ';
if(Validate::url($url, true))
{
echo 'Validation passed.';
}
else
{
echo 'Validation is not passed.';
}
}
// output:
// www.diafancms.com: Validation is not passed.
// http://www.diafancms.com/: Validation passed.