Blogger Information
Blog 7
fans 0
comment 0
visits 4683
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
表单的验证---0416
李昊文叔叔的博客
Original
545 people have browsed it

不会js...没法写 先学学吧

实例

 switch ($_GET['check']) {
 	//验证邮箱
 	case 'email': 		
		$email = $_POST['email']; // 设置默认值
		if (empty($email)) {
			exit(json_encode(['status'=>0,'msg'=>'邮箱不能为空']));
		} else if (in_array($email, ['admin@php.cn','zhu@php.cn'])){
			exit(json_encode(['status'=>1,'msg'=>'邮箱已占用']));
		} else {
			echo json_encode(['status'=>2,'msg'=>'邮箱可用']);
		}
 		break;

 	//验证密码
 	case 'password1':
 		$password1 = $_POST['password1'];
		if (empty($password1)) {
			exit(json_encode(['status'=>0,'msg'=>'密码不能为空']));
		}
		break; 	

	//验证确认密码
 	case 'password2':
 		$password1 = $_POST['password1'];
 		$password2 = $_POST['password2'];
		if (empty($password2)) {
			exit(json_encode(['status'=>0,'msg'=>'确认不能为空']));
		} else if ($password1 != $password2){
			exit(json_encode(['status'=>1,'msg'=>'二次密码不相等']));
		}  else {
			exit(json_encode(['status'=>2,'msg'=>'验证通过']));
		}
		break; 	

	//简介验证(仅做非空验证)
	case 'comment':
		$comment = $_POST['comment'];
		if (empty($comment)) {
			exit(json_encode(['status'=>0,'msg'=>'简介不能为空']));
		} else if (mb_strlen(trim($comment)) < 10) {
			exit(json_encode(['status'=>1,'msg'=>'长度小于10个字符']));
		} else {
			exit(json_encode(['status'=>2,'msg'=>'通过']));
		}

 	//提交验证 
 	case 'submit':
 		//因为数据之前已经全部验证,这里直接返回结果即可
 		exit('恭喜,注册成功');

运行实例 »

点击 "运行实例" 按钮查看在线实例


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post