该验证脚本如何工作?

时间:2016-05-05 05:49:06

标签: javascript html html5

我有以下验证脚本可以正常工作,但我不明白它是如何工作的,需要你的帮助。

HTML,

<form action="xyz.php" method="post">
    <div class="form-group">
        <label class="sr-only" for="id">Enter ID</label>
        <input type="text" class="form-control" name="id" id="id" required>
    </div>
    <button onclick="return validateID()" type="submit" name="submit" class="btn">Submit</button>
</form>

这是JS,

<script>
function validateID() {
    var id = document.getElementById("id").value;
    var regL= new RegExp("^([0-9][0-9][0-9])$");

    if (!( (regL.test(id)) )) {
        window.location.href = "index-iderror.php";
        return false;
    } else {
        return true;
    }
}
</script>

我对此有一些疑问,

  1. 如果将false或true返回onclick会发生什么?是否决定是否将表单数据发送到xyz.php?
  2. 如何继续执行return false之后的window.location.href行? window.location.href不应该指向停止执行的新页面吗?如果删除return false,则脚本不起作用,即未经检查的表单数据将发送到xyz.php。

0 个答案:

没有答案
相关问题