如何仅检查数字(包括负数)?
如何检查仅从 -10 负数到 +10 正数的数字?
这是我所拥有的,但我认为它不安全:
if(isset($_POST['number']) && ctype_digit($_POST['number']) && $_POST['number']>=-10 && $_POST['number']<=10){
//do something
}
并且形式:
Input a number between -10 and 10: <input type="text" name="number" size="5" />
how can I check for numbers only from -10 negative to +10 positive?
This is what I have, but I think it's not safe:
if(isset($_POST['number']) && ctype_digit($_POST['number']) && $_POST['number']>=-10 && $_POST['number']<=10){
//do something
}
and the form:
Input a number between -10 and 10: <input type="text" name="number" size="5" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
还有其他方法,但只有这个有效。任何无法转换为数字的内容都将被视为零。如果这不是所需的行为,请添加:
到内部 IF 语句,以确保没有从输入中删除非数字字符。
There are other ways, but that one will work. Anything that can't be converted to a number will be treated as zero. If this is not desired behaviour, add:
To that inner IF statement, to ensure that no non-numeric characters were removed from the input.
检查变量是否是包含零和负值的数字
Check whether a variable is a number including zero and negative values