PHP驗證類
<?php
/**
* 驗證類
*/
class Validator {
/*
函數名稱:isNumber
簡要描述:檢查輸入的是否爲數字
輸入:string
輸出:boolean
*/
public static function isNumber($val) {
if (preg_match("/^[0-9]+$/", $val))
return TRUE;
return FALSE;
}
/*
* 函數名稱:isPhone
* 簡要描述:檢查輸入的是否爲電話
* 輸入:string
* 輸出:boolean
*/
public static function isPhone($val) {
//eg: xxx-xxxxxxxx-xxx | xxxx-xxxxxxx-xxx ...
if (preg_match("/^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/", $val))
return TRUE;
return FALSE;
}
/*
* 函數名稱:isMobile
* 簡要描述:檢查輸入的是否爲手機號
* 輸入:string
* 輸出:boolean
*/
public static function isMobile($val) {
//該表達式可以驗證那些不小心把連接符“-”寫出“-”的或者下劃線“_”的等等
if (preg_match("/(^(\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 (preg_match("/^[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 (!preg_matchi("/^[0-9a-z]+[0-9a-z\.-]+[0-9a-z]+$/", $Domain)) {
return FALSE;
}
if (!preg_matchi("/\./", $Domain)) {
return FALSE;
}
if (preg_matchi("/\-\./", $Domain) or preg_matchi("/\-\-/", $Domain) or preg_matchi("/\.\./", $Domain) or preg_matchi("/\.\-/", $Domain)) {
return FALSE;
}
$aDomain = explode(".", $Domain);
if (!preg_matchi("/[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 (preg_match("/^[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 (preg_match("/^[a-zA-Z]{" . $min . "," . $max . "}$/", $val))
return TRUE;
return FALSE;
}
/*
* 函數名稱:isEnglish
* 簡要描述:檢查輸入是否爲英文
* 輸入:string
* 輸出:boolean
*/
public static function isEnglish($theelement) {
if (preg_match("/[\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 (preg_match("/^[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 (preg_match("/^[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 (preg_match("^[0-9]{1,}$", $val))
return TRUE;
if (preg_match("/^[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);
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.