【问题标题】:Trouble with JavaScript Validation on Form表单上的 JavaScript 验证问题
【发布时间】:2018-08-17 20:22:45
【问题描述】:

我正在尝试使用 JavaScript 验证登录表单的某些输入。据我所知,脚本中的所有内容都是正确的,但它不起作用。我确定这是我错过的一件小事,任何帮助将不胜感激。

注意:我无法使用外部库。

JavaScript 代码:

<script>
    function validateForm() {
        var x = document.forms["login"]["username"].value;
        var y = document.forms["login"]["password"].value;
        if (isNaN(x)) && (y === "") {
            alert("Username must be numerical, and password must be entered!");
            return false;
        } else if (isNaN(x)) && (y !== "") {
            alert("Username must be numerical!");
            return false;
        } else if (Number.isInteger(x) == true) && (y === "") {
            alert("Password must be entered!");
            return false;
        } else {
            return true;
        }
    }
</script>

HTML 代码:

<form name="login" method="get">
    <input type="text" placeholder="Customer ID" name="username">
    <input type="password" placeholder="Password" name="password">
    <input type="submit" onclick="return validateForm()" value="Log In" name="button">
    <?php if (isset($errormessage)) { echo $errormessage; } ?>
</form>

(我是网络开发新手,请不要过多判断:p)

【问题讨论】:

  • 改用 jquery。见jqueryvalidation.org/files/demo
  • 说“它不起作用”有点太宽泛了。您能否详细说明实际行为?
  • 我无法为此使用外部库。应该指定的。
  • 我没有得到任何回应。密码为空什么都不做,与用户是文本输入等一样

标签: javascript php html web


【解决方案1】:

除了其他答案:

这一行:var x = document.forms["login"]["username"].value; 将用户名的值存储为一个字符串,即使输入了一个数值。现在我邀请您测试以下代码行:

Number.isInteger('12')

它将返回 false。

其中一个可能的解决方案是在使用它之前在 x 上使用 parseInt :

var x = parseInt(document.forms["login"]["username"].value);

如果给定一个不可解析的非 int 值,它仍然会返回 NaN,如果该值是可解析的,则将其转换为 int。

旁注:

parseInt('a') == NaN
parseInt('12') == 12
parseInt('12a') == 12

【讨论】:

  • 啊,我明白了。谢谢你:)
【解决方案2】:

if 语句的条件周围缺少括号。

    function validateForm() {
        var x = document.forms["login"]["username"].value;
        var y = document.forms["login"]["password"].value;
        if ((isNaN(x)) && (y === "")) {
            alert("Username must be numerical, and password must be entered!");
            return false;
        } else if ((isNaN(x)) && (y !== "")) {
            alert("Username must be numerical!");
            return false;
        } else if ((Number.isInteger(x) == true) && (y === "")) {
            alert("Password must be entered!");
            return false;
        } else {
            return true;
        }
    }
<form name="login" method="get">
    <input type="text" placeholder="Customer ID" name="username">
    <input type="password" placeholder="Password" name="password">
    <input type="submit" onclick="return validateForm()" value="Log In" name="button">
    <?php if (isset($errormessage)) { echo $errormessage; } ?>
</form>

如果您有任何问题,请告诉我。

【讨论】:

  • 谢谢,这在大多数情况下都有效。检查密码为空,但用户名正确无效
  • @ProbablyDownsy 看看我对这部分的回答
  • @ProbablyDownsy 这是因为您的代码没有检查用户名是否为空。您只检查 x isNaN 和 x 是否为整数。在您的代码中添加另一个条件以检查空白。
【解决方案3】:
<script>
   function validateForm() {
   var x = document.forms["login"]["username"].value;
   var y = document.forms["login"]["password"].value;
   if ( (isNaN(x)) && (y === "")) {
        alert("Username must be numerical, and password must be entered!");
        return false;
    } else if ( (isNaN(x)) && (y !== "")) {
        alert("Username must be numerical!");
        return false;
    } else if ( (Number.isInteger(x) == true) && (y === "")) {
        alert("Password must be entered!");
        return false;
    } else {
        return true;
    }
}

您在脚本中遗漏了一些括号,请使用此代码

【讨论】:

    【解决方案4】:

    你的 if 语句中有额外的括号:

    if (isNaN(x))<- this is closing the conditional and is not required
    

    删除它,它应该可以工作:

    if (isNaN(x) && y === "") {
    

    else 分支也有这个问题,整个if 应该是这样的:

    if (isNaN(x) && y === "") {
        alert("Username must be numerical, and password must be entered!");
        return false;
    } else if (isNaN(x) && y !== "") {
        alert("Username must be numerical!");
        return false;
    } else if (Number.isInteger(x) == true && y === "") {
        alert("Password must be entered!");
        return false;
    } else {
        return true;
    }
    

    另外,isNan 函数可以从Number 使用(就像isInteger)而不是全局,这将使脚本更加一致:)

    【讨论】:

    • 好了。非常感谢:)
    猜你喜欢
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 2017-05-17
    • 2015-07-18
    • 2020-02-26
    • 2020-03-03
    相关资源
    最近更新 更多