令人困惑的 Javascript 整数比较错误

发布于 2024-12-10 09:46:09 字数 1051 浏览 3 评论 0原文

这可能是一件非常简单的事情,但我一生都无法弄清楚或找到任何类似问题的东西。我对 Javascript 相当陌生,我正在对 HTML 字段进行一些简单的表单验证。当前的两个问题是:

    if(minSquare.search(intCheck) == -1 || minSquare < 500 || minSquare > 5000 || minSquare > maxSquare)
    {
        errorPrint.innerHTML += "Incorrect minimum square feet amount <br />";
        errors++;
    }

    if(maxSquare.search(intCheck) == -1 || maxSquare > 5000 || maxSquare < 500)
    {
        errorPrint.innerHTML += "Incorrect maximum square feet amount <br />";
        errors++;
    }

minSquare/maxSquare 是从 HTML 字段获取的值。正如您所看到的,平方英尺值(在房地产设置中)应该在 500-5000 之间,并且必须是整数。逻辑是存在的,但每次我提交表单时,如果该值低于 1,000,尽管下限为 500,则会打印不正确的最小平方英尺的错误。 另一对的情况也是如此。如果该值低于 100,000,则不会打印,尽管其下限为 30,000。

由于某种原因,当我输入 500 作为最小金额和 5000 作为最大金额(正确的范围)时,它不会提交任何问题。但如果我输入 501 和 5000,它将抛出最小值错误。我不知道为什么会发生这种情况。我不认为这是浏览器问题; Chrome 和 Firefox 都试过了,同样的问题。任何帮助表示赞赏;问题越愚蠢,我就能越快解决这个问题 =)

编辑: 修复了问题。就知道很简单。根据下面的建议,我只需要解析输入,然后我需要摆脱 RegExp 搜索方法,该方法仅在搜索的变量是字符串时才有效。感谢那些回复的人。

This is probably a really simple thing but I can't for the life of me figure out or find anything remotely like the issue. I'm fairly new to Javascript and I'm doing some simple form validation on HTML fields. The two problems at hand are these:

    if(minSquare.search(intCheck) == -1 || minSquare < 500 || minSquare > 5000 || minSquare > maxSquare)
    {
        errorPrint.innerHTML += "Incorrect minimum square feet amount <br />";
        errors++;
    }

    if(maxSquare.search(intCheck) == -1 || maxSquare > 5000 || maxSquare < 500)
    {
        errorPrint.innerHTML += "Incorrect maximum square feet amount <br />";
        errors++;
    }

minSquare/maxSquare are the values taken from the HTML fields. As you can see the square feet values (in a real estate setting) are supposed to be between 500-5000 and have to be an integer. The logic is there, but every time I submit the form, the error for incorrect minimum square feet prints if the value is below 1,000 despite the lower limit being 500. This is the same case for another pair of fields in that if the value is below 100,000, it won't print despite its own lower limit being 30,000.

For some reason when I enter 500 for the minimum amount and 5000 for the maximum amount (the proper range), it will submit no problem. But if I enter say, 501 and 5000 it will throw an error for the minimum value. I have no idea why this is happening. I don't think it's a browser issue; tried on both Chrome and Firefox, same problem. Any help appreciated; the more stupid the problem the quicker I can close this up =)

EDIT: Fixed the problem. Knew it was simple. As per advice below, I just needed to parse the inputs and then I needed to get rid of the RegExp search method that would only work if the variable being searched is a string. Thanks to those who replied.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文