0

我正在设计一个表格来记录用户的银行详细信息。在提交时,我想确保他们提供了排序代码、帐号和密码的适当详细信息(我知道你永远不应该只为我的项目记录密码!!)。如果任何或所有字段的长度不正确,我希望出现错误。这是我的代码,但目前它似乎没有发现任何问题,也许有人可以指出任何错误?

    <script>
    function formValidator(){
        var bankName = document.getElementById('bankName');
        var accName = document.getElementById('accName');
        var sortcode = document.getElementById('sortcode');
        var accNum = document.getElementById('accNum');
        var balance = document.getElementById('bal');
        var passhint = document.getElementById('passHint');
        var pin = document.getElementById('pin');
        var overdraft = document.getElementById('ODLimit');

        if(sortcodeLength(sortcode, 6)){
            if(accountNumLength(accNum, 8)){
                if(pinLength(pin, 4)){
                    return true;
                }
            }
        }
        return false;
    }

        function sortcodeLength(elem, val){
    var Input = elem.value;
    if(Input.length == val){
        return true;
    }else{
        alert("Please enter 6 digits for your sortcode");
        elem.focus();
        return false;
    }
    }

    function accountNumLength(elem, val){
            var Input = elem.value;
            if(Input.length == val){
                return true;
            }else{
            alert("Please enter 8 digits for your Account Number");
                elem.focus();
                return false;
            }
        }

    function pinLength(elem, val){
    var Input = elem.value;
    if(Input.length == val){
        return true;
    }else{
        alert("Please enter 4 digits for your Pin");
        elem.focus();
        return false;
    }
}
</script>

然后我的表格需要验证

    <form onClick='return formValidator()'>
Bank Name: <input type='text' id='bankName'/><br />
Acc Name: <input type='text' id='accName'/><br />
Sort Code: <input type='text' id='sortCode'/><br />
Acc Num: <input type='text' id='accNum'/><br />
Balance: <input type='text' id='bal'/><br />
PasswordHint: <input type='text' id='passHint'/><br />
Pin: <input type='text' id='pin'/><br />
Overdraft Limit: <input type='text' id='ODLimit'/><br />
<input type='submit' value='Check Form' />
</form>

如果没有发现字段长度的任何错误,我会做错什么?

4

2 回答 2

0

您需要在表单上使用 onsubmit 事件处理程序而不是 onclick

<form onsubmit='return formValidator();'>

查找排序代码字段时,您也有错字。它的大小写错误(导致空引用错误)。

var sortcode = document.getElementById('sortCode');
于 2013-09-26T14:19:42.177 回答
0

将 onclick 事件从表单移至提交按钮,即:onClick='return formValidator()'

<form >
Bank Name: <input type='text' id='bankName'/><br />
Acc Name: <input type='text' id='accName'/><br />
Sort Code: <input type='text' id='sortCode'/><br />
Acc Num: <input type='text' id='accNum'/><br />
Balance: <input type='text' id='bal'/><br />
PasswordHint: <input type='text' id='passHint'/><br />
Pin: <input type='text' id='pin'/><br />
Overdraft Limit: <input type='text' id='ODLimit'/><br />
<input type='submit' value='Check Form' onClick='return formValidator()' />
</form>
于 2013-09-26T14:21:47.100 回答