我希望我的字段只接受整数,并且我已将该字段保留在所需的验证中


I want my field to accept only integers and i have kept that field in required validation?

我已经完成了我的完整项目,但它缺乏这一个要求,我不能在我的项目中自己解决这个问题。

可以使用CodeIgniter的表单验证吗?并设置如下规则:

$this->form_validation->set_rules('yourfield', 'Your field''s human name', 'required|integer');

假设该字段名为yourfield


阅读评论后,似乎您想要检查字段中的值是否使用jQuery为整数。我建议看看这个插件:jQuery - numeric

这里有一个使用它的好例子。

相关代码如下:

如果你有这样的输入:

<input class="integer" type="text" />

然后您可以验证它,检查它是否为整数,如下所示:

$(".integer").numeric(false, function() { alert("Integers only"); this.value = ""; this.focus(); });

然而,由于JavaScript/jQuery是在客户端运行的(并且可以被绕过),我要强调的是,在服务器端也进行验证是非常重要的,在本例中使用PHP。这个出色的回答更详细地解释了为什么两者都很重要。

使用HTML5

<input type="number" min="0" />

<input name="Est_Value" id="estvalue" class="regi_input_1" style="width: 150px; height: 25px;" type="number" min="0" />

使用JavaScript验证

<input name="Est_Value" id="estvalue" class="regi_input_1" style="width: 150px; height: 25px;" type="text" onkeypress="if ( isNaN( String.fromCharCode(event.keyCode) )){ alert('numbers only'); return false; }" />

工作演示http://jsfiddle.net/cse_tushar/FEVrB/