在开发中,通常会遇见简单的表单验证,希望快速获取用户提交信息,但是为了安全考虑,我们不光前端需要做js验证,后端也需要做相应的验证,确保不是恶意提交的信息,以下是php通过正则来验证真实姓名、手机号码、邮箱,希望对你有所帮助。
代码片段如下:
/**
* 验证中文姓名格式
* @author ivcxw.com
* @param $name
* @return boolean
*/
function is_RealName($name) {
if (!is_numeric($name)) {
return false;
}
return preg_match('/^([\xe4-\xe9][\x80-\xbf]{2}){2,4}$/', $name) ? true : false;
}
/**
* 验证手机号格式
* @author ivcxw.com
* @param number $mobile
*/
function is_Mobile($mobile) {
if (!is_numeric($mobile)) {
return false;
}
return preg_match('#^1[3,4,5,7,8,9]{1}[\d]{9}$#', $mobile) ? true : false;
}
/**
* 验证邮箱格式
* @author ivcxw.com
* @param string $str
* @return boolean
*/
function is_Email($email) {
if (!$email) {
return false;
}
return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $email) ? true : false;
}