Need help with input validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brossyg
    New Member
    • Nov 2008
    • 8

    Need help with input validation

    I have a function that calculates price when the user inputs a quantity that works perfectly. Here it is:
    Code:
     
    function STWCORPrice() {
    var price = 0;
    var shipping = 0;
    var total=0;
    var productcost=0;
    var qnty=0;
    var stwqnty=document.getElementById("STWquant").value;
    var corqnty=document.getElementById("CORquant").value;
     
    qnty = (stwqnty*1) + (corqnty*1);
     
    if (qnty > 49 && qnty < 75){
    shipping = 6.25;
    price = .23;
    }
     
    else if (qnty > 74 && qnty < 100){
    shipping = 6.25;
    price = .17;
    }
     
    else if (qnty > 99 && qnty < 3000){
    shipping = 21;
    price = .0925;
    }
     
    else if (qnty > 2999 && qnty < 4000){
    shipping = 27;
    price = .0925;
    }
     
    productcost = price * qnty;
    total = productcost + shipping;
     
    document.all('qnty').innerHTML= qnty.toFixed(0);
    document.all('productcost').innerHTML= '$ '+ productcost.toFixed(2);
    document.all('shipping').innerHTML= '$ '+ shipping.toFixed(2);
    document.all('total').innerHTML= '$ '+ total.toFixed(2);
    }
    However, the function requires a whole, positive number to work. I would like to have an alert that pops up if any of the following occurs as part of the IF statement in the function:

    1: If the number is negative. or in not a whole number, and
    2: If the number is not devisible by 25 (meaning, if IF(qnty/25<>INT(qnty/25))

    How do I include these "IFs" in the function?

    Thanks
    Last edited by Niheel; Nov 23 '08, 10:18 PM. Reason: code tag
  • Aardsquid
    New Member
    • Nov 2008
    • 4

    #2
    First off, I'd get rid of this:
    Code:
    qnty = (stwqnty*1) + (corqnty*1);
    And replace it with this:
    Code:
    qnty = stwqnty.parseInt() + corqnty.parseInt();
    Originally posted by brossyg
    How do I include these "IFs" in the function?
    Before the line parsing the quantity, use a quick regular expression:
    Code:
    if (!(stwqnty + corqnty).match ( /^[0-9]$/ )) {
        // Error message
        return;
    }
    That just makes sure that the string is the characters 0-9 throughout.

    Best regards!
    Last edited by Aardsquid; Nov 23 '08, 10:51 PM. Reason: Not clear

    Comment

    • brossyg
      New Member
      • Nov 2008
      • 8

      #3
      Thanks. How do I add an error message if (qnty/25<>Integer(qnt y/25))? ie...if the qnty does not equal a mulltiple of 25?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        First of all, replace all instances of document.all with document.getEle mentById().

        Secondly, check all values that need to be positive integers with /^\d+$/ (a regular expression for numbers only).

        Finally, use the modulus operator % to check for multiples of 25.

        Comment

        Working...