温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

javascript与php批量验证类

发布时间:2020-07-12 18:22:21 来源:网络 阅读:463 作者:Lee_吉 栏目:web开发

一、javascript:

  1. 代码:
    /**
    * 批量验证类
    * @author [lee] <[<www.dollarphp.com>]>
    * 1、验证是否为空
    * 2、验证数据类型
    * 3、验证长度是否达标
    * 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+"'不能为非数组值";
                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);
  2. 输出:
    [ '\'lee\'长度不能小于5,不能大于20',
    '\'lee\'格式不匹配\'/^a.*/\'',
    '\'hello\'不能为非整型值',
    '\'hello\'长度不能小于8,不能大于20',
    '\'hello\'格式不匹配\'/^a\\s*/\'' ]

    二、php:

  3. 代码:
    <?php
    /**
    * 批量验证类
    * @author [lee] <[<www.dollarphp.com>]>
    * 1、验证是否为空
    * 2、验证数据类型
    * 3、验证长度是否达标
    * 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}'不能为非数组值";
                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);
  4. 输出:
    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*/'"
    }
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI