简体   繁体   English

Javascript验证数字xhtml

[英]Javascript Validating Numbers xhtml

I was working on some javascript homework and it states as follows: 我正在做一些JavaScript作业,它说明如下:

-validate that the Customer ID is a Number, not a string. -验证客户ID是数字,而不是字符串。 Also all Customer IDs are numbers between 3000 and 3999. All 4 digit numbers. 同样,所有客户ID都是3000到3999之间的数字。所有4位数字。 Validate that all Customer IDs are numbers between 3000 and 3999. 验证所有客户ID是3000到3999之间的数字。

-validate that the AcctNo is a Number, not a string. -验证AcctNo是数字,而不是字符串。 And the number is between 90000 and 99999. 该数字在90000和99999之间。

As I was working on the code, the part with the numbers always gave me errors, as I searched on the web for help, it came up with // 当我在编写代码时,带有数字的部分总是给我错误,当我在网络上寻求帮助时,它就提出了//

Any help is nice, thanks (: 任何帮助都很好,谢谢(:

Xhtml XHTML

    <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>

http://jsfiddle.net/2w663kb0/ http://jsfiddle.net/2w663kb0/

use id insted of name in html 在HTML中使用名称插入ID

html html

<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>

js js

$('#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;
    }

Try this: 尝试这个:

var txtVal = parseInt(document.getElementById("CustomerID").value) || 0;

And

var txtVal1 = parseInt(document.getElementById("AcctNo").value) || 0;

Also: 也:

else {//<-- braces,since it has multiple lines
   alert('Please enter a number between 3000-3999');
   return false;
}

There is problem with your else condition 您的else条件有问题

you need to include braces in the else block too So you must have 您还需要在else块中包含大括号,因此您必须

else
     {
        alert('Please enter a number between 3000-3999');
        return false;
     }

and

      else
         {
            alert('Please enter a number between 90000-99999');
            return false;
         }

Also

        var newtxtval = parseInt(txtval);

add the above line in both the functions and it will work 在两个函数中添加以上行,它将起作用

  1. You have the name of the text boxes as CustomerID and AcctNo not the id of the text boxes. 文本框的nameCustomerIDAcctNo不是文本框的id So when your validation function accessing them using document.getElementById method will always give null reference, hence it will give java script error whenever you change the content of the CustomerID text box. 因此,当您的验证函数使用document.getElementById方法访问它们时,将始终提供空引用,因此,每当您更改CustomerID文本框的内容时,它将给出Java脚本错误。

  2. Function validateRa() is not at all called in your code, which is used to validate the account number. 在您的代码中根本没有调用函数validateRa() ,该代码用于验证帐号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM