php正则验证

<?php
header("content-type:text/html;charset=utf8");
//var_dump($_POST);die;
//array(5) { ["uname"]=> string(9) "刘刘" ["uqq"]=> string(10) "1111111111" ["uemail"]=> string(12) "888888@qq.com" ["utel"]=> string(11) "13999999999" ["uinfo"]=> string(48) "地方开始放假开放活动健康的话概括" }
empty($_POST["uname"])?$uname="":$uname=$_POST["uname"];
empty($_POST["uemail"])?$uemail="":$uemail=$_POST["uemail"];
empty($_POST["utel"])?$utel="":$utel=$_POST["utel"];
empty($_POST["uqq"])?$uqq="":$uqq=$_POST["uqq"];
empty($_POST["uinfo"])?$uinfo="":$uinfo=$_POST["uinfo"];
//验证姓名
$reg="/^[\x{4e00}-\x{9fa5}]{2,3}$/u";
if(!preg_match($reg,$uname)){
echo "用户名应该2-3个汉字";die;
//header("refresh:1;url=form.html");
}
//验证邮箱
$reg="/^(\w+@\w+(\.)com|net|cn)$/";
if(!preg_match($reg,$uemail)){
echo "邮箱必须含有@,且以com结尾";header("refresh:1;url=form.html"); die;
}
//验证座机号
$reg="/^\d{11}$/";
if(!preg_match($reg,$utel)){
echo "座机号以010-22222222格式";header("refresh:1;url=form.html"); die;
}
//验证QQ号
$reg="/^\d{5,11}$/";
if(!preg_match($reg,$uqq)){
echo "qq必须是5-11位纯数字";header("refresh:1;url=form.html"); die;
}
//验证简介
/*$reg="/^[\x{4e00}-\x{9fa5}]{10,100}\W+/u";
if(!preg_match($reg,$uinfo)){
echo "简介应该10-100个汉字";die;
//header("refresh:1;url=form.html");
}
*/

//连接数据库
$link=mysql_connect('127.0.0.1','root','root')or die("连接失败");
//选择数据库
mysql_select_db('kaoshi',$link);
//设置字符集
mysql_query("set names utf8");
//写sql语句
$sql="insert into zhuce(c_name,c_qq,c_email,c_tel,c_info) values('$uname','$uqq','$uemail','$utel','$uinfo')";
//echo $sql;die;
$rel=mysql_query($sql);
if($rel){
echo "注册成功";header("refresh:1;url=show.php");

}else{echo "注册失败";header("refresh:1;url=form.html");}









$_POST['yzcode'] = trim( $_POST['yzcode'] );
if ( $_POST['yzcode'] != $_SESSION['saxueCheckCode'] ) {
// 验证码错误
exit( json_encode( array( 'flag' => 102 ) ) );
}
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$_POST['email'])||strlen($_POST['email']) < 8) {
// email错误
exit( json_encode( array( 'flag' => 105 ) ) );
}
if (!preg_match("/1[34578]{1}\d{9}$/", $_POST['dianhua'])) {
// 手机错误
exit( json_encode( array( 'flag' => 106 ) ) );
}




?>


 

如果您觉得我的文章有帮助,请随意赞赏!

*发表评论

用QQ账号登录  请登录后发表评论