测试表单输入是jquery和php的1或2位整数 [英] test if a form input is a 1 or 2 digit integer with jquery and php
问题描述
我的表单有五个字段都设置为maxlength =2。
I have a form that has five fields that are all set to maxlength="2".
基本上,我想要输入的唯一值是一个或两位数的整数,因为在值存储在数据库之前对这些字段执行计算。
Basically, i want the only values that can be entered to either be a one or two digit integer, because calculations are performed on these fields before the values are stored in the database.
是否有任何jquery不会让用户甚至输入一个不是整数的值?
Is there any jquery that will not let a user even enter a value that isnt an integer?
同样用jquery和php验证这个的最佳方法是什么?我找到了一些方法,但我需要确保其安全,避免字符,-2,.1等
Also what would be the best way to validate this with both jquery and php? I have found some ways of doing it, but i need to make sure its secure, avoiding characters, -2, .1 etc
已解决
对于我使用的php部分
For the php part i used
if(!ctype_digit($_POST['value']))
只允许正整数
对于javascript
And for javascript
$('.inputQuantity').keyup(function(){
this.value = this.value.replace(/[^0-9\.]/g,'');
});
删除输入字段中不是数字的任何条目。
which deletes any entry made into the input field that is not a number.
以上两种工作都有,但我认为有些人更喜欢使用focusout(function()而不是keyup(function();)
Both above work, although i think some people prefer to use focusout(function() rather than keyup(function() ;)
推荐答案
已解决
对于我使用的php部分
For the php part i used
if(!ctype_digit($_POST['value']))
只允许正整数
对于javascript
And for javascript
$('.inputQuantity').keyup(function(){
this.value = this.value.replace(/[^0-9\.]/g,'');
});
删除输入字段中不是数字的任何条目。
which deletes any entry made into the input field that is not a number.
以上两种工作都有,但我认为有些人更喜欢使用focusout(function()而不是keyup(function();)
Both above work, although i think some people prefer to use focusout(function() rather than keyup(function() ;)
这篇关于测试表单输入是jquery和php的1或2位整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!