验证数字Javascript

时间:2014-09-27 21:22:40

标签: javascript

到目前为止,我已经编写了代码,并提出了这个问题。我必须

确保用户在文本框中输入数字,并且使用Xhtml格式给出了错误,其中一个是'&&'标志给了我错误,由于在线帮助,我被告知我需要使用//

当我学生学习Javascript时,我不知道这是什么或意味着什么,但当我把它放在那里时,我被给予了更多的错误,我的代码在添加javascript后崩溃了。

感谢您的帮助

    <head>
           <script type = 'text/javascript'>
             //  <![CDATA[
   $('#submit').click(function(){
validateRange();
validateRa();    
})

 function validateRange() {
        var txtVal = document.getElementById("CustomerID").value;
 var txtVal1=parseInt(txtVal);
        if (txtVal1 >= 3000 && txtVal1 <= 3999) {
            return true;
        }
        else {
            alert('Please enter a number between 3000-3999');
            return false;
        }
    }

     function validateRa() {
        var txtVal1 = document.getElementById("AcctNo").value;
         var txtVal2=parseInt(txtVal1);

        if (txtVal2 >= 90000 && txtVal2 <= 99999) {
            return true;
        }
        else {
            alert('Please enter a number between 90000-99999');
            return false;
        }
}
     // ]]
</script>
        <title>Account Lookup</title>


    </head>
    <body>
       <h1> Please Provide Your Information</h1>
    <p><input type="text" id="AcctNo" value="Account Number"/></p>
    <p><input type="text" id="CustomerID" value="CustomerID" onchange="validateRange()"/></p>
    <p><input type="text" name="Type" value="Account Type" onchange="validateRange()"/></p>
    <p><input type="text" name="balance" value="Balance"/></p>


    <p class="submit" />
    <input type="submit" name="commit" value="Submit" id="submit" /><button type="reset" value="Clear">Clear</button></p>
    </body>

</html>

2 个答案:

答案 0 :(得分:0)

<强> EDITED 试着用这个:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>

$(document).ready(function(){

   $('#submit').click(function(){
       validateRange();
validateRa();    
});
});
 function validateRange() {
        var txtVal = document.getElementById("CustomerID").value;
 var txtVal1=parseInt(txtVal);
        if (txtVal1 >= 3000 && txtVal1 <= 3999) {
            return true;
        }
        else {
            alert('Please enter a number between 3000-3999');
            return false;
        }
    }

     function validateRa() {
        var txtVal1 = document.getElementById("AcctNo").value;
         var txtVal2=parseInt(txtVal1);

        if (txtVal2 >= 90000 && txtVal2 <= 99999) {
            return true;
        }
        else {
            alert('Please enter a number between 90000-99999');
            return false;
        }
     }

</script>
<html>

<title>Account Lookup</title>

    <body>
       <h1> Please Provide Your Information</h1>
    <p><input type="text" id="AcctNo" value="Account Number"/></p>
    <p><input type="text" id="CustomerID" value="CustomerID" onchange="validateRange()"/></p>
    <p><input type="text" name="Type" value="Account Type" onchange="validateRange()"/></p>
    <p><input type="text" name="balance" value="Balance" /></p>


    <p class="submit" />
    <input type="submit" name="commit" value="Submit" id="submit" /><button type="reset" value="Clear">Clear</button></p>
    </body>

    </html>

答案 1 :(得分:-1)

BETWEEN函数验证在//]]

之前缺少你需要添加的结束花括号
function validateRa() {
    var txtVal1 = document.getElementById("AcctNo").value;
     var txtVal2=parseInt(txtVal1);

    if (txtVal2 >= 90000 && txtVal2 <= 99999) {
        return true;
    }
    else {
        alert('Please enter a number between 90000-99999');
        return false;
    }
}   //<= this is missing in your code
// ]]