PHP

【PHP】PHP实现金额检测

小破孩
2023-05-08 / 0 评论 / 115 阅读 / 正在检测是否收录...
public function checkMoney($value)
{
    if (!is_numeric($value)) {
        return false;
    }
    if ($value <= 0) {
        return false;
    }
 
    if (preg_match('/^[0-9]+(\.\d{1,2})?$/',$value)) {
        return true;
    } else {
        return false;
    }
}
优化
    public function checkMoney($value)
    {
        if(is_array($value)){
            foreach ($value as $v){
                $this->checkMoney($v);
            }
        }else{
            if (!is_numeric($value)) {
                return $this->show('100','金额不正确');
            }
            if ($value <= 0) {
                return $this->show('100','金额不正确');
            }
            if (preg_match('/^[0-9]+(\.\d{1,2})?$/',$value)) {
                return true;
            } else {
                return $this->show('100','金额不正确');
            }
        }
    }
0

评论 (0)

取消