我希望我的字段只接受整数,我已经保持该字段所需的验证? [英] I want my field to accept only integers and i have kept that field in required validation?
问题描述
我有完整的项目,但它缺乏这一个要求,我不能在我的项目自己解决这个问题。
I have got my complete project done but it is lacking this one requirement and i'm not able to solve this issue in my project myself.
推荐答案
您可以使用CodeIgniter的表单验证吗?并设定如下的规则:
Could you use CodeIgniter's form validation? And set a rule like this:
$this->form_validation->set_rules('yourfield', 'Your field\'s human name', 'required|integer');
这假设字段名为 yourfield
。
阅读评论后,似乎想使用jQuery检查字段中的值是否为整数。建议您查看此插件: jQuery - 数字
After reading the comments, it seems like you want to check if value in the field is an integer using jQuery. I'd suggest looking into this plugin: jQuery - numeric
有一个很好的例子可以使用它此处。
There's a good example of to use it here.
以下是相关代码:
像这样:
<input class="integer" type="text" />
然后您可以验证它,检查它是一个整数,如下:
Then you can validate it, checking that it is an integer, like this:
$(".integer").numeric(false, function() { alert("Integers only"); this.value = ""; this.focus(); });
但是,由于JavaScript / jQuery是在客户端(并且可以绕过),我要强调的是,在服务器端验证,这种情况下使用PHP非常重要。 这个出色的答案更详细地解释了为什么两者都很重要。
However, as JavaScript/jQuery is run on the client-side (and can be bypassed), I would stress that it is very important to also validate on the server-side, in this case using PHP. This excellent answer explains in more detail why both are important.
这篇关于我希望我的字段只接受整数,我已经保持该字段所需的验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!