php 常用验证表单提交类
不说了直接上代码
<?php
/**********************************************
* 整理类型: 常用验证表单提交类 *
* 整理时间: 2012-11-26 *
* 整理人: icyzhl,499375381@qq.com *
*********************************************/
class verify{
#验证用户名,$value传递值;$minLen最小长度;$maxLen最长长度;只允许下划线+汉字+英文+数字(不支持其它特殊字符)
#@param string $value
#@param int $length
#@return boolean
function isUsername($value,$minLen=2,$maxLen=30){
if(!$value) return false;
// preg_match("/^[x80-xffa-zA-Z0-9]{3,60}$/", $val) //只能输入中文英文
// preg_match("/^[\xa0-\xff]{1,4}$/", $string);
// return (preg_match("/^([\x81-\xfe][\x40-\xfe]){".$num1.",".$num2."}$/",$str))?true:false;
return preg_match('/^[_\w\d\x{4e00}-\x{9fa5}]{'.$minLen.','.$maxLen.'}$/iu',$value);
}
#验证是否为指定语言,$value传递值;$charset 默认字符类别(en只能英文;cn只能汉字;alb数字;ALL不限制)
#@param string $value
#@param int $length
#@return boolean
function islanguage($value,$charset='all',$minLen=1,$maxLen=50){
if(!$value) return false;
switch($charset){
case 'en':$match = '/^[a-zA-Z]{'.$minLen.','.$maxLen.'}$/iu';break;
case 'cn':$match = '/^[\x{4e00}-\x{9fa5}]{'.$minLen.','.$maxLen.'}$/iu';break;
case 'alb':$match = '/^[0-9]{'.$minLen.','.$maxLen.'}$/iu';break;
case 'enalb':$match = '/^[a-zA-Z0-9]{'.$minLen.','.$maxLen.'}$/iu';break;
case 'all':$match = '/^[a-zA-Z0-9\x{4e00}-\x{9fa5}]{'.$minLen.','.$maxLen.'}$/iu';break;
//all限制为:只能是英文或者汉字或者数字的组合
}
return preg_match($match,$value);
}
#验证密码,$value传递值;$minLen最小长度;$maxLen最长长度;
#@param string $value
#@param int $length
#@return boolean
function isPassword($value,$minLen=6,$maxLen=16){//支持空格
$match='/^[\\~!@#$%^&*() -_=+|{}\[\],.?\/:;\'\"\d\w]{'.$minLen.','.$maxLen.'}$/i';
$value=trim($value);
if(!$value) return false;
return preg_match($match,$value);
}
/**
* 验证eamil
* @param [type] $value [description]
* @return boolean [description]
*/
function isEmail($value){
if(!$value) return false;
return filter_var($value,FILTER_VALIDATE_EMAIL);
}
#验证URL
function isURL($value){
if(!$value) return false;
return filter_var($value,FILTER_VALIDATE_URL);;
}
#验证IP
function isIP($value){
if(!$value) return false;
return filter_var($value,FILTER_VALIDATE_IP);
}
#格式化money,$value传递值;小数点后最多2位
#@param string $value
#@return boolean
function formatMoney($value){
return sprintf("%1\$.2f",$value);
}
#验证电话号码,$value传递值;$match正则方式
#@param string $value
#@return boolean
function isTelephone($value,$match='/^(0[1-9]{2,3})(-| )?\d{7,8}$/'){
//支持国际版:$match='/^[+]?([0-9]){1,3}?[ |-]?(0[1-9]{2,3})(-| )?\d{7,8}$/'
//支持国际版:([0-9]{1,5}|0)?1([3|4|5|8])+([0-9]){9,10}
if(!$value) return false;
return preg_match($match,$value);
}
#验证手机,$value传递值;$match正则方式
#@param string $value
#@param string $match
#@return boolean
function isMobile($value,$match='/^(0)?1([3|4|5|8])+([0-9]){9,10}$/'){
if(!$value) return false;
return preg_match($match,$value);
}
#验证身份证号码,$value传递值;$match正则方式
#@param string $value
#@param string $match
#@return boolean
function isIDcard($value,$match='/^\d{6}((1[89])|(2\d))\d{2}((0\d)|(1[0-2]))((3[01])|([0-2]\d))\d{3}(\d|X)$/i'){
if(!$value) return false;
else if(strlen($value)>18) return false;
return preg_match($match,$value);
}
//验证是否为指定长度的字母/数字组合
function fun_text1($num1,$num2,$str) {
Return (preg_match("/^[a-zA-Z0-9]{".$num1.",".$num2."}$/",$str))?true:false;
}
//QQ格式验证
function is_qq($qq){
$chars = "/^[1-9]{1}[0-9]{4,8}$/";
if (preg_match($chars, $qq)){
return true;
}
return false;
}
//验证手机号码格式
function is_phone($phone){
$chars = '/(^(13[0-9]|14[57]|15[012356789]|17[678]|18[0-9])\d{8}$)|(^170[059]\d{7}$)/';
// $chars = "/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$/";
if (preg_match($chars, $phone)){
return true;
}
return false;
}
//验证固话格式
function is_tel($tel){
$chars = "/^([0-9]{3,4}-)?[0-9]{7,8}$/";
if (preg_match($chars, $tel)){
return true;
}
return false;
}
/*
函数名称:isNumber
*/
function isNumber($val) {
if(preg_match("^[0-9]+$", $val))
return true;
return false;
}
function isEmpty($val) {
if (!is_string($val)) return false; //是否是字符串类型
if (empty($val)) return false; //是否已设定
if ($val=='') return false; //是否为空
return true;
}
/*
函数名称:isChinese
简要描述:检查是否输入为汉字
*/
static function isChinese($sInBuf){ //正确的函数
if (preg_match("/^[x7f-xff]+$/", $sInBuf)){ //兼容gb2312,utf-8
return true;
}else{
return false;
}
}
/*
函数名称:isDomain($Domain)
简要描述:检查一个(英文)域名是否合法
*/
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;
}
}
$verify=new verify();

浙公网安备 33010602011771号