Home  >  Article  >  Backend Development  >  PHP verification class (validation of email, numbers, strings, etc.)

PHP verification class (validation of email, numbers, strings, etc.)

WBOY
WBOYOriginal
2016-07-25 08:56:431122browse
本文分享一个php 验证类的代码,可用于验证Email地址、验证是否数字、验证日期等。有需要的朋友不妨作个参考。

php 验证类的代码,如下:

<?php 
/**
* 基于服务器端的输入验证
* 包括 Email、数字、日期等的验证
* 编辑整理:程序员之家 bbs.it-home.org
*/

class Validation 
{ 
//检测电子邮件格式是否正确,返回true或false
function is_email($email) 
    { 
        if(!preg_match("/^[A-Za-z0-9\._\-+]+@[A-Za-z0-9_\-+]+(\.[A-Za-z0-9_\-+]+)+$/",$email)) 
            return false; 
        return true; 
    } 
// End of is_email Function 

//检查给定的数字符号/无符号数
//返回true或false。
    function is_number($number) 
    { 
        if(!preg_match("/^\-?\+?[0-9e1-9]+$/",$number)) 
            return false; 
        return true; 
    } 
// End of is_number Function 

//检查否无符号数字
//返回true或false
function is_unsign_number($number) 
    { 
        if(!preg_match("/^\+?[0-9]+$/",$number)) 
            return false; 
        return true; 
    } 
// End of is_unsign_number Function 
     
//检查给定的字符串是否数字、字母等的组合
//返回true或false
    function is_alpha_numeric($str) 
    { 
        if(!preg_match("/^[A-Za-z0-9 ]+$/",$str)) 
            return false; 
        return true; 
    } 
// End of is_alpha_numeric Function 

//这个函数检查给定的日期是有效或无效。
//返回true或false。
function is_date($d) 
    { 
        if(!preg_match("/^(\d){1,2}[-\/](\d){1,2}[-\/]\d{4}$/",$d,$matches)) 
            return -1;//Bad Date Format 
        $T = split("[-/\\]",$d); 
        $MON=array(0,31,28,31,30,31,30,31,31,30,31,30,31); 
        $M = $T[0]; 
        $D = $T[1]; 
        $Y = $T[2]; 
        return $D>0 && ($D<=$MON[$M] ||    $D==29 && $Y%4==0 && ($Y%100!=0 || $Y%400==0));  
    } 
    //End of is_data function 
}
?>

2,php 验证类的调用示例:

<?php 
require("validations.inc.php"); 

$val=new Validation; 

if($val->is_email($_POST['email'])) 
    echo "Email is Valid!"; 
else 
    echo "Not a valid email"; 
echo "<br>"; 

if($val->is_number($_POST['number'])) 
    echo "number is Valid!"; 
else 
    echo "Not a valid number"; 
echo "<br>"; 

if($val->is_alpha_numeric($_POST['String'])) 
    echo "String is Valid!"; 
else 
    echo "Not a valid String"; 
echo "<br>"; 
if($val->is_date($_POST['date'])>0) 
    echo "date is Valid!"; 
else 
    echo "Not a valid date"; 
echo "<br>"; 

?> 
<form name='form1' method='post' action=""> 
<table> 
<tr><td>Enter Email :</td><td><input name='email'></td></tr> 
<tr><td>Enter Number :</td><td><input name='number'></td></tr> 
<tr><td>Enter Date :</td><td><input name='date'>(mm-dd-yyyy)</td></tr> 
<tr><td>Enter String :</td><td><input name='String'></td></tr> 
<tr><td>Press Submit :</td><td><input name='Submit' type='submit'></td></tr> 
</table> 
</form>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn