PHP Codeigniter表单验证(数字)

时间:2018-09-10 09:32:20

标签: php forms codeigniter

我正在尝试为数字输入提供0到20之间的范围

这是代码:

<div class=" col-md-9">
<?php
    echo form_input( array(
        "id"            => "artificial_intelligence",
        "name"          => "average_info_av_ia",
        "class"         => "form-control",
        "type"          => "number",
        "placeholder"   => lang( 'artificial_intelligence' ),
        "min"           => "0",
        "max"           => "20"
    ));
?>
</div>

但它只允许输入:0,1,2,10,11,12,13,14,15,16,17,18,19,20 当我点击3到9之间的数字时,它会达到20 怎么样 ?

1 个答案:

答案 0 :(得分:0)

如果要使用范围,则可以使用如下所示的输入类型范围

"type" => "range",   

或者,如果您仍然使用输入类型编号,则需要使用javascript检查值以确保其介于 1到20 之间。参见下面的代码

<div class=" col-md-12">
<?php
  echo form_input( array(
    "id"            => "artificial_intelligence",
    "name"          => "average_info_av_ia",
    "class"         => "form-control",
    "type"          => "number",
    "placeholder"   => lang( 'artificial_intelligence' ),
    "min"           => "0",
    "max"           => "20",
    "onchange"      => "handleChange(this);"
  ));
?>
</div>  
<script>
  function handleChange(input) {
    if (input.value < 0) input.value = 0;
    if (input.value > 20) input.value = 20;
  }
</script>