dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: Reality Check range 0-99 problem

  1. #1
    Join Date
    Oct 2013
    Posts
    2

    Reality Check range 0-99 problem

    Hi guys,
    I have a program that works fine, but I would like to add another check to the code. I want the user to only be able to enter numbers from 0-99. Can anyone suggest how I would do that. Any help is greatly appreciated.
    Thanks
    Code:
    function totalCost(){
        //declare variables
        var snicker = parseInt(document.getElementById("snicker").value);
    
        var twix = parseInt(document.getElementById("twix").value);
        var chips = parseInt(document.getElementById("chips").value);
        var salesTax = 0.05;
        var total = 0;
      
    
        if (!isNaN(snicker)) {
            total += (snicker * .59);    
        }
     
        if (!isNaN(twix)) {
            total += (twix * .49);
        }
     
        if (!isNaN(chips)) {
            total += (chips * .39);
        }
     
        total += (salesTax * total);
     
        if (total != 0) {
            alert("The total is " + total);
            return false;
        } else {
            alert("Invalid total cost, please make a selection");
        }
    }

  2. #2
    Join Date
    Jun 2008
    Posts
    106

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,595
    If you have a field that is numeric, then you can simply -0 from the string value to return a numeric value, eg.

    [CODE]var twix = document.getElementById("twix").value-0;[?CODE]

    in the HTML side of things, you can limit the number of character that the field will accept to 2 characters. You could attach a script to run on change to check that any character input is a number 0-99 as well.

    All the code you need is on developer.com and only needs searching for.
    If your post falls off the page, bump it. ...
    Please remember to wrap any code you have in forum tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles