我已经完成了我的完整项目,但它缺少这一要求,我自己无法在我的项目中解决这个问题。
你会使用CodeIgniter吗表单验证 http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html?并设定这样的规则:
$this->form_validation->set_rules('yourfield', 'Your field\'s human name', 'required|integer');
这假设该字段被称为yourfield
.
阅读评论后,您似乎想使用 jQuery 检查字段中的值是否为整数。我建议研究一下这个插件:jQuery - 数字 http://www.texotela.co.uk/code/jquery/numeric/
有一个很好的例子来使用它here https://github.com/SamWM/jQuery-Plugins/blob/master/numeric/test.html.
这是相关代码:
如果您有这样的输入:
<input class="integer" type="text" />
然后您可以验证它,检查它是否是一个整数,如下所示:
$(".integer").numeric(false, function() { alert("Integers only"); this.value = ""; this.focus(); });
然而,由于 JavaScript/jQuery 在客户端运行(并且可以被绕过),我想强调的是,在服务器端(在本例中使用 PHP)进行验证也非常重要。这个优秀的答案 https://stackoverflow.com/a/162579/604532更详细地解释了为什么两者都很重要。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)