SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Test is numeric?

    Hi all,

    Im creating a postage calc for an Aussie client, and I need to check the input is numeric before I run it through a switch to decide which State/Territory the postcode belongs too (im guessing thats the best idea).

    What would be the best way to check the input is numeric/integer before I run a test on it?

    Many thanks!

  2. #2
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    This page probably will assist: http://andrewpeace.com/javascript-is-numeric.html
    Ryan B | My Blog | Twitter

  3. #3
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great link, thanks rguy!

  4. #4
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    You're welcome, glad it helped.
    Ryan B | My Blog | Twitter

  5. #5
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    Code:
    <script type="text/javascript">
      function form_input_is_numeric(input){
        return !isNaN(input);
      }
    </script>
    That function will pronunce an empty string to be numeric, so without supporting validation its use is inadvisable.
    Tab-indentation is a crime against humanity.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    If you're getting value from a form, you can just use Number() to turn the string value in to a number.
    Code javascript:
    var postcode = Number(form.elements.postcode.value);

    or, If you want to ensure that it's an integer, parseInt() can be used instead.
    Code javascript:
    var postcode = parseInt(form.elements.postcode.value, 10);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    If you're getting value from a form, you can just use Number() to turn the string value in to a number.
    Code javascript:
    var postcode = Number(form.elements.postcode.value);
    Code:
    Number( "" ) == 0
    or, If you want to ensure that it's an integer, parseInt() can be used instead.
    Code javascript:
    var postcode = parseInt(form.elements.postcode.value, 10);
    That ensures that it starts with an integer.

    Code:
    parseInt( "1oo", 10) == 1
    The most satisfactory solution I've seen is to use isFinite combined with parseFloat to eliminate null strings.
    Code:
    return !isNaN( parseFloat( n ) ) && isFinite( n );
    Tab-indentation is a crime against humanity.


Bookmarks

Posting Permissions

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