您好,登錄后才能下訂單哦!
/**
* 批量驗證類
* @author [lee] <[<www.dollarphp.com>]>
* 1、驗證是否為空
* 2、驗證數(shù)據(jù)類型
* 3、驗證長度是否達標(biāo)
* 4、驗證是否符合正則匹配
*/
function validation(main){
this.data = ''
this.msg = new Array()
this.init = function(){
for(var i in main){
this.data = i;
bool = main[i]['empty'];
type = main[i]['type'];
len_arr = main[i]['length'];
preg = main[i]['preg'];
this.isEmpty(bool).dataType(type).dataLength(len_arr).pregMach(preg)
}
}
this.isInt = function(val){
if((typeof(val)=='number')||(val instanceof Number)){
if(/^\d*$/.test(val)){
return true
}else{
return false
}
}else{
return false
}
}
this.isFloat = function(val){
if((typeof(val)=='number')||(val instanceof Number)){
if(/^\d*\.\d*$/.test(val)){
return true
}else{
return false
}
}else{
return false
}
}
this.isString = function(val){
if((typeof(val)=='string')||(val instanceof String)){
return true
}else{
return false
}
}
this.isBoolean = function(val){
if((typeof(val)=='boolean')||(val instanceof Boolean)){
return true
}else{
return false
}
}
this.isArray = function(val){
if(val instanceof Array){
return true
}else{
return false
}
}
this.isObject = function(val){
if(val instanceof Object && typeof val.length == 'undefined'){
return true
}else{
return false
}
}
this.isEmpty = function (bool){
var data = this.data;
if(bool){
if(data.length == 0){
var msg = "'"+data+"'不能為空";
this.msg.push(msg)
}
}
return this
}
this.dataType = function (type){
var data = this.data
if(type=='bool'){
if(!this.isBoolean(data)){
var msg = "'"+data+"'不能為非布爾值";
this.msg.push(msg)
}
}else if(type=='string'){
if(!this.isString(data)){
var msg = "'"+data+"'不能為非字符串值";
this.msg.push(msg)
}
}else if(type=='int'){
if(!this.isInt(data)){
var msg = "'"+data+"'不能為非整型值";
this.msg.push(msg)
}
}else if(type=='float'){
if(!this.isFloat(data)){
var msg = "'"+data+"'不能為非浮點型值";
this.msg.push(msg)
}
}else if(type=='array'){
if(!this.isArray(data)){
var msg = "'"+data+"'不能為非數(shù)組值";
this.msg.push(msg)
}
}else if(type=='object'){
if(!this.isObject(data)){
var msg = "'"+data+"'不能為非對象值";
this.msg.push(msg)
}
}
return this
}
this.dataLength = function (len_arr){
var data = this.data
if(len_arr){
var min = Math.abs(len_arr[0]);
var max = Math.abs(len_arr[1]);
var isint = this.isInt(data)
var isfloat = this.isFloat(data)
var isstring = this.isString(data)
var isbool = this.isBoolean(data)
var isarray = this.isArray(data)
var isobject = this.isObject(data)
var len = 0
if(isint){
len = data.toString().length
}else if(isfloat){
var len0 = data.toString().split('.')
len = len0[1].length
}else if(isstring){
len = data.length
}else if(isarray){
len = data.length
}else if(isobject){
for(var i in data){
len++
}
}
if(!(len >= min && len <= max)){
var msg = "'"+data+"'長度不能小于"+min+",不能大于"+max;
this.msg.push(msg)
}
}
return this
}
this.pregMach = function (preg){
var data = this.data
if(preg){
if(!preg.test(data)){
var msg = "'"+data+"'格式不匹配'"+preg+"'";
this.msg.push(msg)
}
}
}
this.checkData = function(){
var msg = this.msg;
return msg;
}
}
var data = {
'lee': {
'empty':true,
'type':'string',
'length':new Array(5,20),
'preg':/^a.*/
},
'hello': {
'empty':false,
'type':'int',
'length':new Array(8,20),
'preg':/^a\s*/
}
}
var validation = new validation(data)
validation.init()
var ret = validation.checkData()
console.log(ret);
[ '\'lee\'長度不能小于5,不能大于20',
'\'lee\'格式不匹配\'/^a.*/\'',
'\'hello\'不能為非整型值',
'\'hello\'長度不能小于8,不能大于20',
'\'hello\'格式不匹配\'/^a\\s*/\'' ]
<?php
/**
* 批量驗證類
* @author [lee] <[<www.dollarphp.com>]>
* 1、驗證是否為空
* 2、驗證數(shù)據(jù)類型
* 3、驗證長度是否達標(biāo)
* 4、驗證是否符合正則匹配
*/
class validation{
private $data;
private $msg = array();
public function __construct($main){
foreach($main as $k=>$v){
$this->data = $k;
$bool = $v['empty'];
$type = $v['type'];
$len_arr = $v['length'];
$preg = $v['preg'];
$this->isEmpty($bool)->dataType($type)->dataLength($len_arr)->pregMath($preg);
}
}
private function isEmpty($bool){
$data = $this->data;
if($bool){
if(empty($data)){
$msg = "'{$data}'不能為空";
array_push($this->msg,$msg);
}
}
return $this;
}
private function dataType($type){
$data = $this->data;
if($type=='bool'){
if(!is_bool($data)){
$msg = "'{$data}'不能為非布爾值";
array_push($this->msg,$msg);
}
}elseif($type=='string'){
if(!is_string($data)){
$msg = "'{$data}'不能為非字符串值";
array_push($this->msg,$msg);
}
}elseif($type=='int'){
if(!is_int($data)){
$msg = "'{$data}'不能為非整型值";
array_push($this->msg,$msg);
}
}elseif($type=='float'){
if(!is_float($data)){
$msg = "'{$data}'不能為非浮點型值";
array_push($this->msg,$msg);
}
}elseif($type=='array'){
if(!is_array($data)){
$msg = "'{$data}'不能為非數(shù)組值";
array_push($this->msg,$msg);
}
}elseif($type=='object'){
if(!is_object($data)){
$msg = "'{$data}'不能為非對象值";
array_push($this->msg,$msg);
}
}
return $this;
}
private function dataLength($len_arr){
$data = $this->data;
if($len_arr){
$min = abs($len_arr[0]);
$max = abs($len_arr[1]);
$type = gettype($data);
if($type=='string'){
$len = strlen($data);
}elseif($type=='integer'){
$len = strlen($data);
}elseif($type=='double'){
$len0 = explode('.',$len0);
$len = strlen($len0[1]);
}elseif($type=='array'){
$len = count($data);
}elseif($type=='object'){
$data = (array)$data;
$len = count($data);
}
if(!($len >= $min && $len <= $max)){
$msg = "'{$data}'長度不能小于{$min},不能大于{$max}";
array_push($this->msg,$msg);
}
}
return $this;
}
private function pregMath($preg){
$data = $this->data;
if(!empty($preg)){
if(!preg_match($preg,$data)){
$msg = "'{$data}'格式不匹配'{$preg}'";
array_push($this->msg,$msg);
}
}
}
public function checkData(){
$msg = $this->msg;
return $msg;
}
}
$data = array(
'lee'=>array(
'empty'=>true,
'type'=>'string',
'length'=>array(5,20),
'preg'=>'/^a.*/'
),
'hello'=>array(
'empty'=>false,
'type'=>'int',
'length'=>array(8,20),
'preg'=>'/^a\s*/'
)
);
$validation = new validation($data);
$ret = $validation->checkData();
var_dump($ret);
array(5) {
[0]=>
string(41) "'lee'長度不能小于5,不能大于20"
[1]=>
string(28) "'lee'格式不匹配'/^a.*/'"
[2]=>
string(28) "'hello'不能為非整型值"
[3]=>
string(43) "'hello'長度不能小于8,不能大于20"
[4]=>
string(31) "'hello'格式不匹配'/^a\s*/'"
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。