PHP - 验证类
<?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 - 验证类的更多相关文章
- JavaScript 数据验证类
JavaScript 数据验证类 /* JavaScript:验证类 author:杨波 date:20160323 1.用户名验证 2.密码验证 3.重复密码验证 4.邮箱验证 5.手机号验证 6. ...
- C# 通用验证类 支持 WPF,MVC,Winform
验证方式, 通过继承 IDataErrorInfo接口 和 DataAnnotations 解释标记语言而实现, 为了能在WPF上通用,所了也要继承属性更改通知接口INotifyPropertyC ...
- C# - DataValid数据验证类
从EasyCode 摘取下来的数据验证类 using System; using System.Collections.Generic; using System.Text; namespace Le ...
- php表单数据验证类
非常好用方便的表单数据验证类 <?php //验证类 class Fun{ function isEmpty($val) { if (!is_string($val)) return false ...
- C# System.Attribute(验证类)
本文以一个项目中通用的验证类来举例说明如何使用自定义Attribute来扩展元数据. 在项目中,我们为了保证各个层次之间的松藕合,通常把在各个层次之间传递数据的封装在一个称为实体类的类中,比如Act ...
- JS表单验证类HTML代码实例
以前用的比较多的一个JS表单验证类,对于个人来说已经够用了,有兴趣的可以在此基础上扩展成ajax版本.本表单验证类囊括了密码验证.英文4~10个 字符验证. 中文非空验证.大于10小于100的数字.浮 ...
- 一个PHP常用表单验证类(基于正则)
一个基于正则表达式的PHP常用表单验证类,作者:欣然随风.这个表单判断类的功能有:验证是否为指定长度的字母/数字组合.验证是否为指定长度汉字.身 份证号码验证.是否是指定长度的数字.验证邮件地址.电话 ...
- 做一个牛XX的身份证号验证类(支持15位和18位)
原文:做一个牛XX的身份证号验证类(支持15位和18位) #region 是否合法的中国身份证号码 protected bool IsChineseID() { if (str.Length == 1 ...
- java 数据格式验证类
作为一个前端,懂一点java,php之类的,甚好. 我所在的项目前端采用的就是java的spring mvc框架,所以我们也写java,掐指一算,也快一年了. 前端而言,验证是一个坎,绕不过去的,前面 ...
随机推荐
- 移除Strorefront站点footer上的Storefront Design By WooThemes字样
进入wp-content\themes\storefront\inc\structure\footer.php, 注释掉代码: if ( ! function_exists( 'storefront_ ...
- HTML 总结-表单-表单属性
HTML5 表单属性 HTML5 的新的表单属性 本章讲解涉及 <form> 和 <input> 元素的新属性. 新的 form 属性: autocomplete novali ...
- JavaScript学习笔记2-数组对象
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 快速配置SSH证书登录
环境: 在 CentOS 5/6/7.RHEL 5/6/7 和 Oracle Linux 6/7 上测试通过 使用 ssh-key-gen 命令生成公钥和私钥 用 ssh-copy-id 命令将公钥复 ...
- page分页
首先封装一个分页类 public class Page<T> { /** * 当前页号 */ private int pageNumber; /** * 总条数 */ private in ...
- Windows Phone 8初学者开发—第4部分:XAML简介
原文 Windows Phone 8初学者开发—第4部分:XAML简介 原文地址: http://channel9.msdn.com/Series/Windows-Phone-8-Developme ...
- 链接分析算法之:主题敏感PageRank
链接分析算法之:主题敏感PageRank 前面的讨论提到.PageRank忽略了主题相关性,导致结果的相关性和主题性降低,对于不同的用户,甚至有很大的差别.例如,当搜索“苹果”时,一个数码爱好 ...
- StringIO模块字符串的缓存
StringIO经常被用来作为字符串的缓存,应为StringIO有个好处,他的有些接口和文件操作是一致的,也就是说用同样的代码,可以同时当成文件操作或者StringIO操作.比如: import st ...
- (3)选择元素——(2)文档对象模型(The Document Object Model)
One of the most powerful aspects of jQuery is its ability to make selecting elements in the DOM easy ...
- unix ls命令
[语法]: ls [-RadCxmlnogrtucpFbqisf1] [文件夹或文件......] [说明]: ls 命令列出指定文件夹下的文件,缺省文件夹为当前文件夹 ./,缺省输出顺序为纵向 ...