Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. test for numbers containing a decimal point

test for numbers containing a decimal point

Scheduled Pinned Locked Moved Web Development
tutorialjavascriptregexhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    john_paul
    wrote on last edited by
    #1

    I know that I can use javascript to test for a number using the below, but I am having trouble figuring out how to use javascript to test for the entry of for example 100.00 or 100, the decimal point is messsing me up. Any help would be appreciated. function TestKeyPress( obj, event ) { var curChar = String.fromCharCode( event.keyCode ); var inpStr = obj.value + curChar window.status = ''; obj.title = ''; result = inpStr.match( '^[0-9]+$' ); if ( ! result ) { window.status = 'Please enter only numbers.'; obj.title = window.status; event.returnValue = false; event.cancel = true; } }

    C G 2 Replies Last reply
    0
    • J john_paul

      I know that I can use javascript to test for a number using the below, but I am having trouble figuring out how to use javascript to test for the entry of for example 100.00 or 100, the decimal point is messsing me up. Any help would be appreciated. function TestKeyPress( obj, event ) { var curChar = String.fromCharCode( event.keyCode ); var inpStr = obj.value + curChar window.status = ''; obj.title = ''; result = inpStr.match( '^[0-9]+$' ); if ( ! result ) { window.status = 'Please enter only numbers.'; obj.title = window.status; event.returnValue = false; event.cancel = true; } }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      There's regex in jscript ? '^[0-9]+\.?[0-9]?$' should do it. ( test for number, optional . and then optional number after the . ) Christian Graus - Microsoft MVP - C++

      1 Reply Last reply
      0
      • J john_paul

        I know that I can use javascript to test for a number using the below, but I am having trouble figuring out how to use javascript to test for the entry of for example 100.00 or 100, the decimal point is messsing me up. Any help would be appreciated. function TestKeyPress( obj, event ) { var curChar = String.fromCharCode( event.keyCode ); var inpStr = obj.value + curChar window.status = ''; obj.title = ''; result = inpStr.match( '^[0-9]+$' ); if ( ! result ) { window.status = 'Please enter only numbers.'; obj.title = window.status; event.returnValue = false; event.cancel = true; } }

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        To match either an integer or floating point number, use this pattern: ^(\d+)|(\d*\.\d+)$ The first part (\d+) will match an integer, e.g. one or more digits. The secont part (\d*\.\d+) will match a floating point number, e.g. optinal digits before the decimal point and one or more digits after. --- b { font-weight: normal; }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups