蛋糕php验证百分比,不接受数字字符和特殊字符

时间:2014-07-04 06:46:40

标签: cakephp cakephp-2.0

我想要税率百分比数字字段的蛋糕PHP验证,在该字段中只接受像5.00这样的数字字符。不大于100而不接受空间字符。我尝试一些模式但它不适用于空间字符,(! #$%^)。我的演示代码是

$this->validate['TaxPercent'] = array(
        'pattern' => array(
                'rule'    => '/^[\0-9]+$/',
                'allowEmpty' => true,
                'message' => __('err__numberfield', array(__('lbl_TaxPercent', true))), 
            ),
        'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => __('err_required', array(__('lbl_TaxPercent', true))), 
            ),
        'decimal' => array(
                'rule' => array('decimal', 2),
                'allowEmpty' => true,
                'message' => __("lbl_TaxPercentValid", array(__('lbl_TaxPercent', true))),
            ),
        'range' => array(
                'rule' => array('range', 0, 101),
                'allowEmpty' => true,
                'message' => __("err__percentage", array(__('lbl_TaxPercent', true))),
            )
        );

所以请建议我适当的解决方案。

1 个答案:

答案 0 :(得分:0)

更新和工作

  $this->validate['TaxPercent'] = array(
    'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => __('err_required', array(__('lbl_TaxPercent', true))), 
        ),
    'decimal' => array(
            'rule' =>  array("decimal", "2",  '/^[0-9]+(\.[0-9]{1,2})?$/') //this is working
            'allowEmpty' => true,
            'message' => __("lbl_TaxPercentValid", array(__('lbl_TaxPercent', true))),
        ),
    'range' => array(
            'rule' => array('range', 0, 100.01), //updated this one to so its only until 100.00
            'allowEmpty' => true,
            'message' => __("err__percentage", array(__('lbl_TaxPercent', true))),
        )
    );