【发布时间】:2013-05-14 08:24:20
【问题描述】:
我已经完成了我的完整项目,但它缺少这一要求,我自己无法在我的项目中解决这个问题。
【问题讨论】:
标签: php jquery codeigniter
我已经完成了我的完整项目,但它缺少这一要求,我自己无法在我的项目中解决这个问题。
【问题讨论】:
标签: php jquery codeigniter
你能用 CodeIgniter 的form validation 吗?并设置这样的规则:
$this->form_validation->set_rules('yourfield', 'Your field\'s human name', 'required|integer');
这假定该字段名为yourfield。
阅读 cmets 后,您似乎想使用 jQuery 检查字段中的值是否为整数。我建议研究一下这个插件:jQuery - numeric
有一个很好的例子来使用它here。
以下是相关代码:
如果您有这样的输入:
<input class="integer" type="text" />
然后你可以验证它,检查它是否是一个整数,像这样:
$(".integer").numeric(false, function() { alert("Integers only"); this.value = ""; this.focus(); });
然而,由于 JavaScript/jQuery 在客户端运行(并且可以被绕过),我要强调在服务器端进行验证也非常重要,在这种情况下使用 PHP。 This excellent answer 更详细地解释了为什么两者都很重要。
【讨论】:
jquery.numeric.js 脚本吗?一个问题是您将 classes 和 ids 混淆了,您需要将脚本的这部分 $(".estvalue") 更改为 $("#estvalue")。如果您编辑原始问题以包含您的格式化代码,而不是仅仅将其转储到评论中,这也会很有帮助。
<input type="number" min="0" />
<input name="Est_Value" id="estvalue" class="regi_input_1" style="width: 150px; height: 25px;" type="number" min="0" />
<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; }" />
【讨论】: