Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Dec 2003
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check for input to be numbers

    Hi,
    I have created the following codes

    function read_display_temp() {
    // read temperature here using prompt() function
    var q2=prompt("Enter the temperature")

    // display temperature on the button
    document.q2form.q2button.value=q2+" Degree"

    }

    How do I make sure that the user enters a number and not letters?

    Lincoln

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I usually check it like this:

    if(parseInt(q2,10)==q2){''is a number''}

  • #3
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    or

    if (!isNAN(q2)){
    //its a number
    }

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    In prompt dialog, null is returned if the [Cancel] button is clicked. Empty string is returned if [OK] button is clicked while the input is empty. And isNaN('') or isNaN(null) returns false which is wrong.

    So it's should be

    if (q2!='' && q2!=null && !isNaN(q2)){
    //its a number
    }

    Maybe Garadon's solution is better or using regex is the best.
    Last edited by glenngv; 05-27-2004 at 03:14 AM. Reason: corrected typo
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    null and '' are both turned into 0 when coerced into numbers, so why not just let be treated that way?
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Rex
    Rex is offline
    New Coder
    Join Date
    May 2004
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello all -

    As far as I know, the name of the function is isNaN, not "isNAN".

    - Rex
    Sincerely,
    Rex

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by liorean
    null and '' are both turned into 0 when coerced into numbers, so why not just let be treated that way?
    Because it's more clear by explicity checking for the different conditions. Additionally I'd write the check as an independent function seeing how we check for all these different conditions. If/when the language spec changes, we've already encapsulated where our changes will occur.


  •  

    Posting Permissions

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