2

在我的脚本中,我需要使用逗号和点以及最大值和最小值来验证价格。这是我的规则()

return [
        [['price'], 'required', 'message' => 'Price ...'],
        [['price'], 'number', 'numberPattern' => '/^[0-9]{1,2}([\.,][0-9]{1,2})*$/',
            'message' => 'Price ...', 'max' => 25, min => '0'],
    ];

当我输入像 25.00(.dot)这样的价格时效果很好,但是当我输入 25,01(,逗号)时,验证不起作用。你知道如何让它工作吗?

4

1 回答 1

1

我发现此解决方案适用于所有输入,您无需寻找特定的小部件选项。在您的视图文件(在底部首选)中注册 JS:

$this->registerJs("        
    $(document).ready(function() {
        $(document).on('keyup', 'input', function(e){
            $(this).val($(this).val().replace(/[,]/g, '.'));
        });
    });
");

这会将所有输入中的所有逗号更改为。我已经测试了自己(当然)并且运行良好。

但是,如果您想以仅在某些输入中应用的方式进行更改,则必须为每个输入添加一个自定义类,然后将此代码稍微更改为:

$this->registerJs("        
    $(document).ready(function() {
        $(document).on('keyup', '.CustomClassName', function(e){
            $(this).val($(this).val().replace(/[,]/g, '.'));
        });
    });
");

我认为这比使用小部件选项更好,因为您将需要找到这样的选项(您甚至不知道这个选项是否甚至存在于第一位)而只要您不忘记,这个选项将始终存在添加自定义类并注册此 JavaScript 代码。

于 2016-08-11T20:38:57.680 回答