【问题标题】:How to check if a text box is not empty and the value is less than 500如何检查文本框是否为空且值小于500
【发布时间】:2012-04-26 05:51:23
【问题描述】:

我有一个文本框,它是供用户输入数字的可选字段。如果里面有数字,我想检查以确保它小于 500,然后对其进行处理。

这是我目前正在做的事情:

if($textbox!="" && <=500)
{
//action here
}

我尝试用 andif 替换 && 但仍然收到错误 Parse error: syntax error, unexpected T_IS_SMALLER_OR_EQUAL

最简单的方法是什么?

【问题讨论】:

    标签: php if-statement


    【解决方案1】:

    您需要在两个语句中都使用该变量 preg_match('/\d/', $textbox ) == 1 将确保它是一个 int

    if($textbox!="" && $textbox <= 500 && preg_match('/\d/', $textbox ) == 1)
    {
    //action here
    }
    

    【讨论】:

    • 将正则表达式引入其中是矫枉过正的。就个人而言,我建议使用:if (!empty($textbox) &amp;&amp; intval($textbox) &lt;= 500) { /* CODE */ }
    • 是的,我注意到我忘记在其中输入is_numeric,但现在编辑已经太晚了。
    【解决方案2】:

    你错过了小于的左边部分

    if($textbox!="" && $textbox <=500)
    {
    //action here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多