简体   繁体   中英

Javascript Validating Numbers xhtml

I was working on some javascript homework and it states as follows:

-validate that the Customer ID is a Number, not a string. Also all Customer IDs are numbers between 3000 and 3999. All 4 digit numbers. Validate that all Customer IDs are numbers between 3000 and 3999.

-validate that the AcctNo is a Number, not a string. And the number is between 90000 and 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

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

use id insted of name in 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

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

you need to include braces in the else block too So you must have

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

  2. Function validateRa() is not at all called in your code, which is used to validate the account number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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