PHP后台验证类(原创)

 

  主要实现的是用户输入内容进行验证,本文是继 ajax提交form,同时提供form表单验证,直接贴上代码

<?php
/**
 * Form check class
 * 格式: uname = array('require'=>'true','max'=>3,'min'=>'1','theme'=> '用户名',reg="tel/qq/num/cn/en/reg")
 * @author Administrator
 *
 */
class Form{
	private $_form_data = array();
	private  $_error_msg = '';
	private $_default_regs = array('num'=>'/^[0-9]*$/', 
							   'tel'=>'/^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/',
							   'qq'=>'/^\d{5,10}$/', 
							   'cn'=>'', 
							   'en'=>'', 
							   'email'=>'/^[0-9a-zA-Z]+@(([0-9a-zA-Z]+)[.])+[a-z]{2,4}$/i');
	public function  Form($form_data = ''){
		$this->_form_data = $form_data;
	}
	public function check(){
		$is_check = true;
		foreach ($this->_form_data as $key=>$item){
			$is_check = $this->check_item($item);
			if(!$is_check){
				break;
			}
		}
		return $is_check;
	}
	public function  init_form($form_data){
		$this->_form_data = $form_data;
	}
	public function check_item($item){
		$is_check = true;
		foreach ($item as $key=>$obj){
			$check_func = 'check_'.$key;
			if(method_exists($this, $check_func)){
				$is_check = $this->$check_func($item);
			}
			if(!$is_check){
				break;
			}
		}
		return $is_check;
	}
	
	public function error_msg(){
		return $this->_error_msg;
	}
	private function check_require($item){
		$require_exp_true = $item['require'] == true && !empty($item['value']);
		if($require_exp_true || $item['require']==false){
			return true;
		}
		return $this->init_error($item['theme'].'不能为空!');
	}
	
	private function check_max($item){
		$str_length = strlen($item['value']);
		if($str_length <= $item['max']){
			return true;
		}
		return $this->init_error($item['theme'].'最多'.$item['max'].'个字符');
	}
	
	private function check_min($item){
		$str_length = strlen($item['value']);
		if($str_length >= $item['min']){
			return true;
		}
		return $this->init_error($item['theme'].'最少'.$item['min'].'个字符');
	}
	
	private function check_reg($item){
		if(array_key_exists($item['reg'], $this->_default_regs)){
			return $this->reg_defaults($item);
		}
		return $this->reg_express($item);
	}
	private function reg_defaults($item){
		$reg = $this->_default_regs[$item['reg']];
		if(preg_match($reg,$item['value'])){
			return true;
		}
		return $this->init_error($item['theme'].'格式不对');
	}
	private function reg_express($item){
		
	}
	private function init_error($msg){
		$this->_error_msg = $msg;
		return false;
	}
}

   

   使用方法

   1、init_form()初始化数据

        数据格式

$form_data = array('username'=> array('require'=>true, 'theme'=> '用户名', 'value'=> $data['username']),
						   'passwd'=> array('require'=>true, 'theme'=> '密码', 'value'=> $data['passwd']),
						   'code'=> array('require'=>true, 'theme'=> '验证码', 'value'=> $data['code'], 'max'=> 4 , 'min'=>4, 'reg'=>'num'));
		

 

  2、check()方法验证,返回true/false

 3、error_msg(),返回验证失败信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值