class MyValidate {
Const REQUIRE_REG = "/.+/";
Const EMAIL = "/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/";
Const PHONE = "/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/";
Const MOBILE = "/^((\(\d{3}\))|(\d{3}\-))?1\d{10}$/";
Const URL = "/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/";
Const IDCARD = "/^\d{15}(\d{2}[A-Za-z0-9])?$/";
Const CURRENCY = "/^\d+(\.\d+)?$/";
Const NUMBER = "/^\d+$/";
Const ZIP = "/^[1-9]\d{6}$/";
Const QQ = "/^[1-9]\d{4,13}$/";
Const INTEGER = "/^[-\+]?\d+$/";
Const DOUBLE = "/^[-\+]?\d+(\.\d+)?$/";
Const ENGLISH = "/^[A-Za-z]+$/";
Const CHINESE = "/^[\u0391-\uFFE5]+$/";
/**
* NULL和空验证
*
* @param $str
*/
public function require_validate($str) {
return preg_match(self::REQUIRE_REG, $str);
}
/**
* 邮箱验证
*
* @param $str
*/
public function email_validate($str, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str))
return FALSE;
return preg_match(self::EMAIL, $str);
}
/**
* 电话验证
*
* @param $str
*/
public function phone_validate($str, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str))
return FALSE;
return preg_match(self::PHONE, $str);
}
/**
* 移动电话验证
*
* @param $str
*/
public function mobile_validate($str, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str))
return FALSE;
return preg_match(self::MOBILE, $str);
}
/**
* url验证
*
* @param $str
*/
public function url_validate($str, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str))
return FALSE;
return preg_match(self::URL, $str);
}
/**
* 身份证号验证
*
* @param $str
*/
public function idcard_validate($str, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str))
return FALSE;
return preg_match(self::IDCARD, $str);
}
/**
* 货币验证
*
* @param $str
*/
public function currency_validate($str, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str))
return FALSE;
return preg_match(self::CURRENCY, $str);
}
/**
* 数字验证
*
* @param $str
*/
public function number_validate($str, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str))
return FALSE;
return preg_match(self::NUMBER, $str);
}
/**
* zip验证
*
* @param $str
*/
public function zip_validate($str, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str))
return FALSE;
return preg_match(self::ZIP, $str);
}
/**
* QQ验证
*
* @param $str
*/
public function qq_validate($str, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str))
return FALSE;
return preg_match(self::QQ, $str);
}
/**
* 整型验证
*
* @param $str
*/
public function integer_validate($str, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str))
return FALSE;
return preg_match(self::INTEGER, $str);
}
/**
* 双整型验证
*
* @param $str
*/
public function double_validate($str, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str))
return FALSE;
return preg_match(self::DOUBLE, $str);
}
/**
* 英文验证
*
* @param $str
*/
public function english_validate($str, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str))
return FALSE;
return preg_match(self::ENGLISH, $str);
}
/**
* 中文验证
*
* @param $str
*/
public function chinese_validate($str, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str))
return FALSE;
return preg_match(self::CHINESE, $str);
}
/**
* 检查长度
*
* @param $str
* @param $minlen
* @param $maxlen
* @param $require
*/
public function lenlimit_validate($str, $minlen, $maxlen, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str))
return FALSE;
if (strlen($str)>=$minlen && strlen($str)<=$maxlen)
return TRUE;
return FALSE;
}
/**
* 检查两字符串是否相同
*
* @param $str1
* @param $str2
* @param $require
*/
public function equals_validate($str1, $str2, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str1))
return FALSE;
if ($str1 == $str2)
return TRUE;
return FALSE;
}
/**
* 检查范围
*
* @param $str
* @param $minval
* @param $maxval
* @param $require
*/
public function range_validate($str, $minval, $maxval, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str))
return FALSE;
if ($str>=$minval && $str<=$maxval)
return TRUE;
return FALSE;
}
/**
* 检查日期
*
* @param unknown_type $str
* @param unknown_type $require
*/
public function date_validate($str, $require = TRUE) {
if ($require==TRUE && !$this->require_validate($str))
return FALSE;
try {
return strtotime($str);
} catch (Exception $e) {
echo 'catch..';
return FALSE;
}
}
/**
* 正则表达式校验
*
* @param $str
*/
public function reg_validate($str, $reg) {
if (preg_match($reg, $str))
return TRUE;
return FALSE;
}
}