当我尝试添加产品并允许我插入字母时,我的代码会发生什么?

时间:2017-06-25 20:10:00

标签: php codeigniter-3

我想要有效货币只有数字,十进制用逗号和点,但当我想尝试添加一个字母form_validation添加仍然它应该发送给我错误而不是添加它,我做错了什么?我该如何修复并且不允许添加字母只是数字和货币格式?

使用money_callback输出测试

1,000.53 TRUE
    50.00 TRUE
    34.3r6 FALSE
    w45.00 FALSE
    a FALSE
    6,355,725.00 TRUE
    355,725.00 TRUE
    725.00 TRUE

callback_function

 function money($number){
        return preg_match("/^(?:[0-9]{1,3})(?:,[0-9]{3})*(?:|\.[0-9]+)$/", $number) ? "TRUE" : "FALSE";
    }

测试

function 
        public function tests(){
           echo $this->money("1,000.53");
           echo $this->money("50.00");
           echo $this->money("34.3r6");
           echo $this->money("w45.00");
           echo $this->money("a");
           echo $this->money("6,355,725.00");
           echo $this->money("355,725.00");
           echo $this->money("725.00");
        }

form_validation codeigniter

$this->form_validation->set_rules('description', 'description', 'trim|alpha_numeric_spaces|required');
        $this->form_validation->set_rules('purchase_price', 'purchase_price', 'callback_money|numeric|required');
        $this->form_validation->set_rules('sale_price', 'sale_price', 'callback_money|numeric|required');
        $this->form_validation->set_rules('wholesale_price', 'wholesale_price', 'callback_money|numeric|required');
        $this->form_validation->set_rules('min_stock', 'min_stock', 'numeric|required');
        $this->form_validation->set_rules('stock', 'stock', 'trim|numeric|required');
        $this->form_validation->set_rules('max_stock', 'max_stock', 'numeric|required');
        $this->form_validation->set_rules('provider_id', 'provider_id', 'numeric|required');
        $this->form_validation->set_message('purchase_price', 'Please enter a valid phone number');
        $this->form_validation->set_message('sale_price', 'Please enter a valid phone number');
        $this->form_validation->set_message('wholesale_price', 'Please enter a valid phone number');
        if ($this->form_validation->run() == FALSE) {
            $this->json($this->form_validation->error_array());
        } else {
            if ($data['stock'] > $data['max_stock']) {
                $this->json(array('max_stock' => 'el stock no puede ser mayor al max'));
            } else {
                if (!$this->products->isExistsProduct($data)) {
                    $this->products->addProduct($data);
                    $this->json(array('msg' => 'successfully added'));
                } else {
                    $this->json(array('duplicate' => 'product already exists'));
                }
            }
        }

0 个答案:

没有答案