www.webdeveloper.com
Results 1 to 3 of 3

Thread: Reality Check range 0-99 problem

Hybrid View

  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,148
    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.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

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