<?php

/**
* 验证类
*/
class Validator {
/*
函数名称:isNumber
简要描述:检查输入的是否为数字
输入:string
输出:boolean
*/ public static function isNumber($val) {
if (ereg("^[0-9]+$", $val))
return TRUE;
return FALSE;
} /*
* 函数名称:isPhone
* 简要描述:检查输入的是否为电话
* 输入:string
* 输出:boolean
*/ public static function isPhone($val) {
//eg: xxx-xxxxxxxx-xxx | xxxx-xxxxxxx-xxx ...
if (ereg("^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$", $val))
return TRUE;
return FALSE;
} /*
* 函数名称:isMobile
* 简要描述:检查输入的是否为手机号
* 输入:string
* 输出:boolean
*/ public static function isMobile($val) {
//该表达式可以验证那些不小心把连接符“-”写出“-”的或者下划线“_”的等等
if (ereg("(^(\d{2,4}[-_-—]?)?\d{3,8}([-_-—]?\d{3,8})?([-_-—]?\d{1,7})?$)|(^0?1[35]\d{9}$)", $val))
return TRUE;
return FALSE;
} /*
* 函数名称:isPostcode
* 简要描述:检查输入的是否为邮编
* 输入:string
* 输出:boolean
*/ public static function isPostcode($val) {
if (ereg("^[0-9]{4,6}$", $val))
return TRUE;
return FALSE;
} /*
* 函数名称:isEmail
* 简要描述:邮箱地址合法性检查
* 输入:string
* 输出:boolean
*/ public static function isEmail($val, $domain = "") {
if (!$domain) {
if (preg_match("/^[a-z0-9-_.]+@[\da-z][\.\w-]+\.[a-z]{2,4}$/i", $val)) {
return TRUE;
} else
return FALSE;
}
else {
if (preg_match("/^[a-z0-9-_.]+@" . $domain . "$/i", $val)) {
return TRUE;
} else
return FALSE;
}
} //end func /*
* 函数名称:isName
* 简要描述:姓名昵称合法性检查,只能输入中文英文
* 输入:string
* 输出:boolean
*/ public static function isName($val) {
if (preg_match("/^[\x80-\xffa-zA-Z0-9]{3,60}$/", $val)) {//2008-7-24
return TRUE;
}
return FALSE;
} //end func /*
* 函数名称:isDomain($Domain)
* 简要描述:检查一个(英文)域名是否合法
* 输入:string 域名
* 输出:boolean
*/ public static function isDomain($Domain) {
if (!eregi("^[0-9a-z]+[0-9a-z\.-]+[0-9a-z]+$", $Domain)) {
return FALSE;
}
if (!eregi("\.", $Domain)) {
return FALSE;
} if (eregi("\-\.", $Domain) or eregi("\-\-", $Domain) or eregi("\.\.", $Domain) or eregi("\.\-", $Domain)) {
return FALSE;
} $aDomain = explode(".", $Domain);
if (!eregi("[a-zA-Z]", $aDomain[count($aDomain) - 1])) {
return FALSE;
} if (strlen($aDomain[0]) > 63 || strlen($aDomain[0]) < 1) {
return FALSE;
}
return TRUE;
} /*
* 函数名称:isNumberLength($theelement, $min, $max)
* 简要描述:检查字符串长度是否符合要求
* 输入:mixed (字符串,最小长度,最大长度)
* 输出:boolean
*/ public static function isNumLength($val, $min, $max) {
$theelement = trim($val);
if (ereg("^[0-9]{" . $min . "," . $max . "}$", $val))
return TRUE;
return FALSE;
} /*
* 函数名称:isNumberLength($theelement, $min, $max)
* 简要描述:检查字符串长度是否符合要求
* 输入:mixed (字符串,最小长度,最大长度)
* 输出:boolean
*/ public static function isEngLength($val, $min, $max) {
$theelement = trim($val);
if (ereg("^[a-zA-Z]{" . $min . "," . $max . "}$", $val))
return TRUE;
return FALSE;
} /*
* 函数名称:isEnglish
* 简要描述:检查输入是否为英文
* 输入:string
* 输出:boolean
*/ public static function isEnglish($theelement) {
if (ereg("[\x80-\xff].", $theelement)) {
return FALSE;
}
return TRUE;
} /*
* 函数名称:isChinese
* 简要描述:检查是否输入为汉字
* 输入:string
* 输出:boolean
*/ public static function isChinese($sInBuf) {
$iLen = strlen($sInBuf);
for ($i = 0; $i < $iLen; $i++) {
if (ord($sInBuf{$i}) >= 0x80) {
if ((ord($sInBuf{$i}) >= 0x81 && ord($sInBuf{$i}) <= 0xFE) && ((ord($sInBuf{$i + 1}) >= 0x40 && ord($sInBuf{$i + 1}) < 0x7E) || (ord($sInBuf{$i + 1}) > 0x7E && ord($sInBuf{$i + 1}) <= 0xFE))) {
if (ord($sInBuf{$i}) > 0xA0 && ord($sInBuf{$i}) < 0xAA) {
//有中文标点
return FALSE;
}
} else {
//有日文或其它文字
return FALSE;
}
$i++;
} else {
return FALSE;
}
}
return TRUE;
} /*
* 函数名称:isDate
* 简要描述:检查日期是否符合0000-00-00
* 输入:string
* 输出:boolean
*/ public static function isDate($sDate) {
if (ereg("^[0-9]{4}\-[][0-9]{2}\-[0-9]{2}$", $sDate)) {
return TRUE;
} else {
return FALSE;
}
} /*
* 函数名称:isTime
* 简要描述:检查日期是否符合0000-00-00 00:00:00
* 输入:string
* 输出:boolean
*/ public static function isTime($sTime) {
if (ereg("^[0-9]{4}\-[][0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$", $sTime)) {
return TRUE;
} else {
return FALSE;
}
} /*
* 函数名称:isMoney($val)
* 简要描述:检查输入值是否为合法人民币格式
* 输入:string
* 输出:boolean
*/ public static function isMoney($val) {
if (ereg("^[0-9]{1,}$", $val))
return TRUE;
if (ereg("^[0-9]{1,}\.[0-9]{1,2}$", $val))
return TRUE;
return FALSE;
} /*
* 函数名称:isIp($val)
* 简要描述:检查输入IP是否符合要求
* 输入:string
* 输出:boolean
*/ public static function isIp($val) {
return (bool) ip2long($val);
} }

  

<?php
/**
* 验证类
*
* @lastmodify 2014-5-16
* @author jy625
*/
class VerifyAction{
/**
* 是否为空值
*/
public static function isEmpty($str){
$str = trim($str);
return !empty($str) ? true : false;
}
/**
* 数字验证
* param:$flag : int是否是整数,float是否是浮点型
*/
public static function isNum($str,$flag = 'float'){
if(!self::isEmpty($str)) return false;
if(strtolower($flag) == 'int'){
return ((string)(int)$str === (string)$str) ? true : false;
}else{
return ((string)(float)$str === (string)$str) ? true : false;
}
}
/**
* 名称匹配,如用户名,目录名等
* @param:string $str 要匹配的字符串
* @param:$chinese 是否支持中文,默认支持,如果是匹配文件名,建议关闭此项(false)
* @param:$charset 编码(默认utf-8,支持gb2312)
*/
public static function isName($str,$chinese = true,$charset = 'utf-8'){
if(!self::isEmpty($str)) return false;
if($chinese){
$match = (strtolower($charset) == 'gb2312') ? "/^[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_-]+$/" : "/^[x{4e00}-x{9fa5}A-Za-z0-9_]+$/u";
}else{
$match = '/^[A-za-z0-9_-]+$/';
}
return preg_match($match,$str) ? true : false;
}
/**
* 邮箱验证
*/
public static function isEmail($str){
if(!self::isEmpty($str)) return false;
return preg_match("/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i",$str) ? true : false;
}
//手机号码验证
public static function isMobile($str){
$exp = "/^13[0-9]{1}[0-9]{8}$|15[012356789]{1}[0-9]{8}$|18[012356789]{1}[0-9]{8}$|14[57]{1}[0-9]$/";
if(preg_match($exp,$str)){
return true;
}else{
return false;
}
}
/**
* URL验证,纯网址格式,不支持IP验证
*/
public static function isUrl($str){
if(!self::isEmpty($str)) return false;
return preg_match('#(http|https|ftp|ftps)://([w-]+.)+[w-]+(/[w-./?%&=]*)?#i',$str) ? true : false;
}
/**
* 验证中文
* @param:string $str 要匹配的字符串
* @param:$charset 编码(默认utf-8,支持gb2312)
*/
public static function isChinese($str,$charset = 'utf-8'){
if(!self::isEmpty($str)) return false;
$match = (strtolower($charset) == 'gb2312') ? "/^[".chr(0xa1)."-".chr(0xff)."]+$/"
: "/^[x{4e00}-x{9fa5}]+$/u";
return preg_match($match,$str) ? true : false;
}
/**
* UTF-8验证
*/
public static function isUtf8($str){
if(!self::isEmpty($str)) return false;
return (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$word)
== true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$word)
== true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$word)
== true) ? true : false;
}
/**
* 验证长度
* @param: string $str
* @param: int $type(方式,默认min <= $str <= max)
* @param: int $min,最小值;$max,最大值;
* @param: string $charset 字符
*/
public static function length($str,$type=3,$min=0,$max=0,$charset = 'utf-8'){
if(!self::isEmpty($str)) return false;
$len = mb_strlen($str,$charset);
switch($type){
case 1: //只匹配最小值
return ($len >= $min) ? true : false;
break;
case 2: //只匹配最大值
return ($max >= $len) ? true : false;
break;
default: //min <= $str <= max
return (($min <= $len) && ($len <= $max)) ? true : false;
}
}
/**
* 验证密码
* @param string $value
* @param int $length
* @return boolean
*/
public static function isPWD($value,$minLen=6,$maxLen=16){
$match='/^[\\~!@#$%^&*()-_=+|{}\[\],.?\/:;\'\"\d\w]{'.$minLen.','.$maxLen.'}$/';
$v = trim($value);
if(empty($v))
return false;
return preg_match($match,$v);
}
/**
* 验证用户名
* @param string $value
* @param int $length
* @return boolean
*/
public static function isNames($value, $minLen=2, $maxLen=16, $charset='ALL'){
if(empty($value))
return false;
switch($charset){
case 'EN': $match = '/^[_\w\d]{'.$minLen.','.$maxLen.'}$/iu';
break;
case 'CN':$match = '/^[_\x{4e00}-\x{9fa5}\d]{'.$minLen.','.$maxLen.'}$/iu';
break;
default:$match = '/^[_\w\d\x{4e00}-\x{9fa5}]{'.$minLen.','.$maxLen.'}$/iu';
}
return preg_match($match,$value);
}
/**
* 验证邮箱
* @param string $value
*/
public static function checkZip($str){
if(strlen($str)!=6){
return false;
}
if(substr($str,0,1)==0){
return false;
}
return true;
}
/**
* 匹配日期
* @param string $value
*/
public static function checkDate($str){
$dateArr = explode("-", $str);
if (is_numeric($dateArr[0]) && is_numeric($dateArr[1]) && is_numeric($dateArr[2])) {
if (($dateArr[0] >= 1000 && $timeArr[0] <= 10000) && ($dateArr[1] >= 0 && $dateArr[1] <= 12) && ($dateArr[2] >= 0 && $dateArr[2] <= 31))
return true;
else
return false;
}
return false;
}
/**
* 匹配时间
* @param string $value
*/
public static function checkTime($str){
$timeArr = explode(":", $str);
if (is_numeric($timeArr[0]) && is_numeric($timeArr[1]) && is_numeric($timeArr[2])) {
if (($timeArr[0] >= 0 && $timeArr[0] <= 23) && ($timeArr[1] >= 0 && $timeArr[1] <= 59) && ($timeArr[2] >= 0 && $timeArr[2] <= 59))
return true;
else
return false;
}
return false;
}
}

  

php validator classes的更多相关文章

  1. Hibernate Validator 6.0.9.Final - JSR 380 Reference Implementation: Reference Guide

    Preface Validating data is a common task that occurs throughout all application layers, from the pre ...

  2. wx

    wx The classes in this module are the most commonly used classes for wxPython, which is why they hav ...

  3. [转]Supporting OData Query Options in ASP.NET Web API 2

    本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/suppor ...

  4. rails提供的validators

    Instance Public methods attribute_method?(attribute)Link Returns true if attribute is an attribute m ...

  5. yii 核心类classes.php详解(持续更新中...)

    classes.php在yii运行的时候将被自动加载,位于yii2文件夹底下. <?php /** * Yii core class map. * * This file is automati ...

  6. jQuery.validator 详解二

    前言:上一篇详细的介绍了jQuery.validator( 版本v1.13.0 )的验证规则,这一篇重点讲述它的源码结构,及如何来对元素进行验证,错误消息提示的内部实现 一.插件结构(组织方式) 在讲 ...

  7. jQuery.validator 详解

    jQuery.validator 详解二 前言:上一篇详细的介绍了jQuery.validator( 版本v1.13.0 )的验证规则,这一篇重点讲述它的源码结构,及如何来对元素进行验证,错误消息提示 ...

  8. spring boot 1.4默认使用 hibernate validator

    spring boot 1.4默认使用 hibernate validator 5.2.4 Final实现校验功能.hibernate validator 5.2.4 Final是JSR 349 Be ...

  9. jQuery validator plugin之Plugin Method

    原文 .validate() validate( [options ] ) options Type: Object debug (default: false) Type: Boolean Enab ...

随机推荐

  1. 使用jquery-validate校验表单

    注意: 表单校验(validation校验[需要下载JQuery-validate插件,而且必须要在引入JQuery插件之后,再引入validate插件/*validate是建立在JQuery之上*/ ...

  2. Bootstrap历练实例:默认的媒体对象

    Bootstrap 多媒体对象(Media Object) 本章我们将讲解 Bootstrap 中的多媒体对象(Media Object).这些抽象的对象样式用于创建各种类型的组件(比如:博客评论), ...

  3. VC下的C语言程序随机数的产生

    本文章适用于VC编译器,VC编译器里有个rand()函数,我们用它来实现取随机数. #include <stdio.h> #include<stdlib.h> //随机数的头文 ...

  4. pandas中数据聚合【重点】

    数据聚合 数据聚合是数据处理的最后一步,通常是要使每一个数组生成一个单一的数值. 数据分类处理: 分组:先把数据分为几组 用函数处理:为不同组的数据应用不同的函数以转换数据 合并:把不同组得到的结果合 ...

  5. Linux常用文档操作命令--2

    4.文档压缩与解压操作 在Linux中常见的压缩文件有:*.tar.gz.*.tgz.*.gz.*.Z.*bz2等.其每种不同的压缩文件对印的压缩和解压命令也不同. *.tar.gz :tar程序打包 ...

  6. 老男孩Python高级全栈开发工程师三期完整无加密带课件(共104天)

    点击了解更多Python课程>>> 老男孩Python高级全栈开发工程师三期完整无加密带课件(共104天) 课程大纲 1.这一期比之前的Python培新课程增加了很多干货:Linux ...

  7. 在WIN2008R2的IIS7环境下安装PHP5.6.15

    1.下载PHP5.6.15 在http://windows.php.net/download页面中找到VC11 x64 Non Thread Safe下载ZIP版. 2.将下载的压缩包解压到D盘PHP ...

  8. Python 简单购物程序

    # Author:Eric Zhao# -*- coding:utf-8 -*-'''需求:启动程序后,让用户输入工资,然后打印商品列表允许用户根据商品编号购买商品用户选择商品后,检测余额是否够,够就 ...

  9. GoF23种设计模式之结构型模式之组合模式

    一.概述 将对象组合成树型结构以表示“部分--整体”的层次关系.组合模式使得用户对单个对象和组合对象的使用具有一致性. 二.适用性 1.你想表示对象的部分--整体层次结构的时候. 2.你希望用户忽略组 ...

  10. Thonny -- 简洁的 python 轻量级 IDE

    Thonny目前是 树莓派 上 默认的 Python 开发环境. 该 IDE 是 Institute of Computer Science of University of Tartu (爱沙尼亚 ...