美文网首页
PHP常用的一些正则验证规则

PHP常用的一些正则验证规则

作者: 刘禹锡_c886 | 来源:发表于2020-11-14 11:42 被阅读0次

1、验证手机号码 正确返回 true

/**
 * 验证手机号码 正确返回 true
 */
function checkPhone($phone){
    if(strlen($phone)!=11){   return false;   }
    if(preg_match("/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$|17[0-9]{1}[0-9]{8}$/",$phone)){
        return true;
    }else{
        return false;
    }
}

2、验证固定电话

/**
 *验证固定电话格式 正确返回 true
 */
function checkTel($tel) {
    if(preg_match("/^([0-9]{3,4}-)?[0-9]{7,8}$/", $tel)) {
        return true;
    } else {
        return false;
    }
}

3、验证邮箱格式

/**
 * 验证邮箱格式 正确返回 true
 */
function checkEmail($email) {
    $chars = "/^[0-9a-zA-Z]+(?:[\_\.\-][a-z0-9\-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\.[a-zA-Z]+$/i";
    if (preg_match($chars, $email)) {
        return true;
    } else {
        return false;
    }
}

4、验证身份证号码格式

/**
 * 验证身份证号码格式 正确返回 true
 */
function checkIdCard($id_card) {
    $chars = "/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$/";
    if (preg_match($chars, $id_card)) {
        return true;
    } else {
        return false;
    }
}

5、验证银行卡号码格式

/**
 * 验证银行卡号码格式 正确返回 true
*/
function checkBank($bank) {
    $chars = "/^(\d{16}|\d{19}|\d{17})$/";
    if (preg_match($chars, $bank)) {
        return true;
    } else {
        return false;
    }
}

6、验证IP地址

/**
 * 验证IP地址 正确返回 true
*/
function checkIP($ip) {
    if(preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $ip)) {
        return true;
    } else {
        return false;
    }
}

7、验证中国邮政编码 6位数字

/**
 * 验证中国邮政编码 6位数字 正确返回 true
*/
function checkPost($post) {
    if(preg_match("/^[1-9]\d{5}(?!\d)$/", $post)) {
        return true;
    } else {
        return false;
    }
}

8、验证用户密码(以字母开头,长度在6-18之间,只能包含字符、数字和下划线)

/**
 * 验证用户密码 正确返回 true
*/
function checkPassword($password) {
    if(preg_match("/^[a-zA-Z]\w{5,17}$/", $password)) {
        return true;
    } else {
        return false;
    }
}

9、验证QQ号

/**
 * 验证QQ号 正确返回 true
*/
function checkQQ($qq) {
    if(preg_match("/^[1-9][0-9]{4,}$/", $qq)) {
        return true;
    } else {
        return false;
    }
}

10、验证网址URL

/**
 * 验证网址URL 正确返回 true
*/
function checkUrl($url) {
    if(preg_match("/http:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is", $url)) {
        return true;
    } else {
        return false;
    }
}

11、验证汉子

/**
 * 验证汉子 正确返回 true
*/
function checkStr($str) {
    if(preg_match("/^[\u4e00-\u9fa5],{0,}$/", $str)) {
        return true;
    } else {
        return false;
    }
}

12、验证是否是数字(可带小数点的数字)

/**
 * 验证是否是数字(可带小数点的数字) 正确返回 true
*/
function checkNum($num) {
    if(is_numeric($num)) {
        return true;
    } else {
        return false;
    }
}

待续......

相关文章

网友评论

      本文标题:PHP常用的一些正则验证规则

      本文链接:https://www.haomeiwen.com/subject/vdnibktx.html