目录
回复内容:
首页 后端开发 php教程 这个PHP验证类如何使用?

这个PHP验证类如何使用?

Jun 06, 2016 pm 08:30 PM
php

比如我写好的POST提交例子,验证邮箱和值是否未空?

<code>include("check.php");

  $check = new VerifyAction();

  if(?){
   echo '提交成功!';
  }else{
    echo '填写不对 重填写';
 }



<form action="index.php" method="post">
   <input type="text" name="name"><br>
   <input type="text" name="email"><br>
   <input type="submit">
   </form>


</code>
登录后复制
登录后复制

验证类 check.php

<code><?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;
    } 
}
</code>
登录后复制
登录后复制

回复内容:

比如我写好的POST提交例子,验证邮箱和值是否未空?

<code>include("check.php");

  $check = new VerifyAction();

  if(?){
   echo '提交成功!';
  }else{
    echo '填写不对 重填写';
 }



<form action="index.php" method="post">
   <input type="text" name="name"><br>
   <input type="text" name="email"><br>
   <input type="submit">
   </form>


</code>
登录后复制
登录后复制

验证类 check.php

<code><?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;
    } 
}
</code>
登录后复制
登录后复制

<code>include("check.php");
$check = new VerifyAction();
//验证邮箱
$check_email = $check->isEmail($_POST['email']);
if($check_email)
{
  //是正确的邮箱
}else{
 //邮箱填写有问题...
}

</code>
登录后复制

不知道你明白了么?如果不明白 加群讨论下告诉你 群:231566327

<code>include("check.php");

 if($_SERVER['REQUEST_METHOD']=='POST'){
     if(!VerifyAction::isEmpty($_POST['name'])){
        die("用户名不能为空") ;
     }elseif(!VerifyAction::isEmail($_POST['email'])){
        die( "无效邮箱账号");
     }
     echo "验证通过";
 }
</code>
登录后复制
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

CakePHP 项目配置 CakePHP 项目配置 Sep 10, 2024 pm 05:25 PM

在本章中,我们将了解CakePHP中的环境变量、常规配置、数据库配置和电子邮件配置。

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 日期和时间 CakePHP 日期和时间 Sep 10, 2024 pm 05:27 PM

为了在 cakephp4 中处理日期和时间,我们将使用可用的 FrozenTime 类。

CakePHP 文件上传 CakePHP 文件上传 Sep 10, 2024 pm 05:27 PM

为了进行文件上传,我们将使用表单助手。这是文件上传的示例。

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

在本章中,我们将学习以下与路由相关的主题?

讨论 CakePHP 讨论 CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 Dec 20, 2024 am 11:31 AM

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

CakePHP 创建验证器 CakePHP 创建验证器 Sep 10, 2024 pm 05:26 PM

可以通过在控制器中添加以下两行来创建验证器。

See all articles