【问题标题】:why does javascript skip some of my functions?为什么javascript会跳过我的一些功能?
【发布时间】:2013-09-20 08:38:38
【问题描述】:

当我运行这个程序时 validatePhone();, validateAddress();和验证城市();完全跳过,为什么?这是我的 JS:

function validatePage()
{
    var valid = false;//sets valid.
    var msg = "";//sets message to blank.
    validateFname();
    function validateFname()
    {
        var fnameTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
        if(firstName.value.match(fnameTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validateLname();
    }
    function validateLname()
    {
        var lnameTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
        if(lastName.value.match(lnameTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validatePhone();
    }
    function validatePhone()
    {
        var nameTxt = /^[0-9]+$///sets valid inputs for recipient name.
        if(Phone.value.match(nameTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validateAddress();
    }
    function validateAddress()
    {
        var addressTxt = /^[0-9a-zA-Z]+$///sets valid inputs for recipient name.
        if(address1.value.match(addressTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validateCity();
    }
    function validateCity()
    {
        var cityTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
        if(cityTown.value.match(cityTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validatePostcode();
    }
    function validatePostcode()
    {
        var postcodeTxt = /^[0-9a-zA-Z]+$///sets valid inputs for recipient name.
        if(postcode.value.match(postcodeTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
    }
    if(valid == true)
    {
        window.open("checkout_step_5.html");
    }
    else
    {
        msg="Not all required fields were filled."
        alert(msg);
        return false;
    }
}

我检查了拼写错误,没有一个我注意到我真的不知道为什么这不起作用?

【问题讨论】:

  • “跳过”究竟是什么意思?
  • 向我们展示一些 html 代码并定义“跳过”。
  • 您是否使用过调试器来查看是否发生异常?现在不可能真正知道会发生什么。
  • 您是否建议它调用 validatePostcode 而不是 validateCity?这似乎不太可能
  • 作为一个疯狂的猜测,您希望valid 在最后一个字段正常时为真,还是在所有 个字段都正常时为真?

标签: javascript html function validation skip


【解决方案1】:

尝试将整个代码放入 try catch 到现在我没有收到任何异常

    <!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function validatePage()
{
try{
    var valid = false;//sets valid.
    var msg = "";//sets message to blank.
    validateFname();
    function validateFname()
    {
        var fnameTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
        if(firstName.value.match(fnameTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validateLname();
    }
    function validateLname()
    {
        var lnameTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
        if(lastName.value.match(lnameTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validatePhone();
    }
    function validatePhone()
    {
        var nameTxt = /^[0-9]+$///sets valid inputs for recipient name.
        if(Phone.value.match(nameTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validateAddress();
    }
    function validateAddress()
    {
        var addressTxt = /^[0-9a-zA-Z]+$///sets valid inputs for recipient name.
        if(address1.value.match(addressTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validateCity();
    }
    function validateCity()
    {
        var cityTxt = /^[a-zA-Z]+$///sets valid inputs for recipient name.
        if(cityTown.value.match(cityTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
        validatePostcode();
    }
    function validatePostcode()
    {
        var postcodeTxt = /^[0-9a-zA-Z]+$///sets valid inputs for recipient name.
        if(postcode.value.match(postcodeTxt))//checks if there has been an entered value, then sets valid to true.
        {
            valid = true;
        }
        else
        {
            valid = false;
        }
    }
    if(valid == true)
    {
        window.open("checkout_step_5.html");
    }
    else
    {
        msg="Not all required fields were filled."
        alert(msg);
        return false;
    }
    }catch(e){
    alert(e);
    }
}


</script>
</head>
<body onload="validatePage()">

</body>
</html>

另外一个建议,如果Valid一旦变为false,请使用return突破该函数。如果一个参数为假,总是检查没有意义。

if(firstName.value.match(fnameTxt))//checks if there has been an entered value, then sets valid to true.
    {
        valid = true;
    }
    else
    {
        valid = false;
return;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多