【问题标题】:codeigniter form validationcodeigniter 表单验证
【发布时间】:2011-05-25 19:51:27
【问题描述】:

我有以下使用 codeigniter 框架的 php 代码

$this->form_validation->set_rules('money', 'Money', 'integer|required|xss_clean');

它将货币字段验证为整数。如何验证字段为整数或十进制数。

我猜它会很简单

$this->form_validation->set_rules('money', 'Money', '(decimal||integer)|required|xss_clean');

但它不起作用!

【问题讨论】:

  • 始终记住,结合 Wesley 所说,您始终可以在设置的规则验证之外添加其他验证。例如:$array = array('money' => filter_var('$this->input->post('money'), FILTER_VALIDATE_INT));。这只是一个示例,并不适合您的特定需求。

标签: validation forms codeigniter


【解决方案1】:

来自 CI Form Validation docs:

注意:您还可以使用任何允许一个参数的原生 PHP 函数。

is_numeric() 可以为您工作,但它接受一些与金钱非常不同的格式。

is_float() 可以工作,但它会在字符串和整数上失败。

这两个函数在验证通常接受为货币值的数字整数或小数时也过于宽松。内置 CI decimal() 函数需要小数点并允许+ 和- 字符。

是的,我知道 - 没有帮助,但希望它能让你思考。您想使用的语法根本不起作用。我建议通过扩展 Form_validation 库来创建自己的表单验证规则来验证资金。

创建文件/application/libraries/MY_Form_validation.php

类似这样的:

class MY_Form_validation extends CI_Form_validation {

    function is_money($input = '')
    {
            // Validate input here and return TRUE or FALSE
    }

}

我最初写了一个损坏的示例,然后我尝试写一些可行的东西,并意识到这取决于你。您可能希望也可能不希望允许使用 $、€、-、+ 等字符,或者超过两位的小数,或者只有一位的小数,或者数字中的逗号分隔千位。 . 使用您认为合适的任何验证方法。

这里有一个关于货币格式验证的好例子:How to check if an entered value is currency

【讨论】:

    【解决方案2】:

    直接使用数字....数字|必需

    【讨论】:

    • 或许更适合发表评论?
    【解决方案3】:

    这是我在 MY_Form_validation 类中使用的。

    /**
     * Money, decimal or integer
     *
     * @access  public
     * @param   string
     * @return  bool
     */
    public function money($str)
    {
        //First check if decimal
        if(!parent::decimal($str))
        {
            //Now check if integer
            return (bool) parent::integer($str);
        }
    
        return TRUE;
    }
    

    当然,在您的 /system/language/english/form_validation_lang.php 中,您应该为验证错误添加适当的消息。

    【讨论】:

      【解决方案4】:

      您可以尝试自定义回调:

      $this->form_validation->set_rules('money', 'Money', 'callback_money_type');
      
      
      function money ($param) {
      
       //conditional statements here
      
      if(is_int($param) || is_float($param){
       $this->form_validation->set_message('money_type', 'Your message here');
        return false;
      
      } else {
      
        return true;
      }
      
      }
      

      【讨论】:

        【解决方案5】:

        ctype_digit 可能有用,它是一个内置的 PHP 函数。

        【讨论】:

          【解决方案6】:

          现在我们可以使用numeric 来验证数字字符,而不是使用decimal||integer。

          【讨论】:

            【解决方案7】:

            你可以像下面这样使用

            $this->form_validation->set_rules('reward', 'Reward', 'callback_validate_money');

            public function validate_money ($input) {
                if(preg_match('/^[0-9]*\.?[0-9]+$/', $input)){
                    return true;
                } else {
                     $this->form_validation->set_message('validate_money','Please enter valid reward!');
                    return false;
                }
            }
            

            【讨论】:

              猜你喜欢
              • 2017-02-06
              • 2016-09-05
              • 2021-10-09
              • 1970-01-01
              • 1970-01-01
              • 2021-03-10
              相关资源
              最近更新 更多