PHP Codeigniter Form Validation (number)



我试图给数字输入一个介于 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 如何?

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

"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>

相关内容

  • 没有找到相关文章

最新更新