手机号验证:
<?php
$tel = "13012345678";
if (!preg_match("/^1[3456789]\d{9}$/", $tel)) {
echo "手机号不正确";
}
?>
验证手机号,座机号:
/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/ 010-88719994这种格式的座机
验证年龄数字:
if(!preg_match("/^\d*$/",$data['age'])||$data['age']>=150){
$this->error = '请填写正确年龄';
return false;
}
邮箱验证:
<?php
if (!preg_match('/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/', $email)) {
echo "邮箱不合法";
}
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailMsg = "正确邮箱格式";
}
?>
URL验证:
$url = strtolower(trim($url ));
if(empty($url )) {
echo "url格式不正确";
}
$match = '/^(http:\/\/)?(https:\/\/)?([\w\d\-]+\.)+[\w\-]+(\/[\d\w\-.\/?%&=]*)?$/';
if (!preg_match($match, $url)) {
echo "url格式不正确";
}
身份证号验证:
$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 (!preg_match($match, $idcard)) {
echo "身份证号不正确";
}
电话号码验证:
$match = '/^0[0-9]{2,3}[-]?\d{7,8}$/';
if (!preg_match($match, $phone)) {
echo "电话号码不正确";
}
用户名验证:
$match = '/^[a-zA-Z_][a-zA-Z0-9-_]{5,19}$';
if (!preg_match($match, $username)) {
echo "用户名格式不正确";
}
密码验证:
$match = '/^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$).{6,16}$/';
if (!preg_match($match, $pwd)) {
echo "密码格式不正确";
}
IP验证:
$match = '/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/';
if (!preg_match($match, $ip)) {
echo "IP格式不正确";
}