如果多个文本框清空验证,则会出错

时间:2012-10-11 07:55:08

标签: javascript html

我使用以下代码限制文本框仅输入数字但是当我尝试验证文本框时不应该为空它不起作用

    function AllowNumbersOnly(input, kbEvent) {       
            var keyCode, keyChar;
            keyCode = kbEvent.keyCode;
            if (window.event)
                keyCode = kbEvent.keyCode; 
            else
                keyCode = kbEvent.which;              
            if (keyCode == null) return true;            
            keyChar = String.fromCharCode(keyCode);
            var charSet = "0123456789";           
            if (charSet.indexOf(keyChar) != -1) return true;           
            if (keyCode == null || keyCode == 0 || keyCode == 8 || keyCode == 9 || keyCode == 13 || keyCode == 27) return true;
            return false;
            if (String.IsNullOrEmpty(txtTextBox.Text))
            {
                MessageBox.Show("Enter Value Please.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);           
            }
            else
            {
                alert("Fill the fields")
            }
        }
   </SCRIPT>

我的HTML代码是

    <form name="requirement" method="post" id="requirement" action="" enctype="multipart/form-data" class="niceform">
<input id="b_size1" name="b_size1" onkeypress="return AllowNumbersOnly(this,event)" type="text"  size="4" />
    <input id="b_size2" name="b_size2" onkeypress="return AllowNumbersOnly(this,event)" type="text"  size="4" />
    <input id="b_size3" name="b_size3" onkeypress="return AllowNumbersOnly(this,event)" type="text"  size="4" />
    <input id="b_size4" name="b_size4" onkeypress="return AllowNumbersOnly(this,event)" type="text"  size="4" /> 
    <input id="b_size5" name="b_size5" onkeypress="return AllowNumbersOnly(this,event)" type="text"  size="4" />
    <input id="b_sizeL" name="b_sizeL" onkeypress="return AllowNumbersOnly(this,event)" type="text"  size="4" />
    <input id="b_size6" name="b_size6" onkeypress="return AllowNumbersOnly(this,event)" type="text"  size="4" />
    <input type="submit" class="btn btn-green" value="Submit">

请指导我为此提供什么解决方案?

1 个答案:

答案 0 :(得分:0)

您的代码未到达您检查其是否为空的部分。你总是通过它上面的return false;退出你的功能。