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. Help needed with javascript function for validating textbox data

Help needed with javascript function for validating textbox data

Scheduled Pinned Locked Moved Web Development
regexjavascripthelpquestion
4 Posts 2 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.
  • B Offline
    B Offline
    Brendan Vogt
    wrote on last edited by
    #1

    Hi, I am either looking for a Javascript function, or a regular expression that I can use in Javascript.   I have been struggling to get mine to work, so I am asking if someone can please help me write a function. What it must do is to allow only 3 decimal places.   While I type the textbox must be validated.   Something like 123.456 is valid.   It should not allow anything after .456 It must accept a number or a float. It must also allow a - and a +. This is what I currently have (but it accepts lots of decimals): var regex = /^(\d|-)*\.?\d*$/; Please can someone help? Thanks Brendan

    R 1 Reply Last reply
    0
    • B Brendan Vogt

      Hi, I am either looking for a Javascript function, or a regular expression that I can use in Javascript.   I have been struggling to get mine to work, so I am asking if someone can please help me write a function. What it must do is to allow only 3 decimal places.   While I type the textbox must be validated.   Something like 123.456 is valid.   It should not allow anything after .456 It must accept a number or a float. It must also allow a - and a +. This is what I currently have (but it accepts lots of decimals): var regex = /^(\d|-)*\.?\d*$/; Please can someone help? Thanks Brendan

      R Offline
      R Offline
      RyanMorris
      wrote on last edited by
      #2

      Hi Brendan, This might help: ^[-+]?[0-9]\d{0,2}(\.\d{1,3})?$ - this will allow numbers like 1, -1.1, 123.456 but not 1234.234, 123.4567 or 1234.4567. ^[-+]?[0-9]\d{2}(\.\d{3})?$ - this will only allow numbers like 123.456 or -123.246. I hope this helps. Ryan

      B 1 Reply Last reply
      0
      • R RyanMorris

        Hi Brendan, This might help: ^[-+]?[0-9]\d{0,2}(\.\d{1,3})?$ - this will allow numbers like 1, -1.1, 123.456 but not 1234.234, 123.4567 or 1234.4567. ^[-+]?[0-9]\d{2}(\.\d{3})?$ - this will only allow numbers like 123.456 or -123.246. I hope this helps. Ryan

        B Offline
        B Offline
        Brendan Vogt
        wrote on last edited by
        #3

        Thanks Ryan, I will check it out soon.   Just another question: RyanMorris wrote: but not 1234.234, 123.4567 or 1234.4567. What's wrong with 1234.234? You don't by any chance have some formatting script that when the textbox looses its focus that it formats the numbers to something like 1234.560 even if 1234.56 was typed in?   I want it to be formatted to 3 digits. 123 becomes 123.000, 123.4 becomes 123.400 etc. Does the above regular expressions accept only digits?

        R 1 Reply Last reply
        0
        • B Brendan Vogt

          Thanks Ryan, I will check it out soon.   Just another question: RyanMorris wrote: but not 1234.234, 123.4567 or 1234.4567. What's wrong with 1234.234? You don't by any chance have some formatting script that when the textbox looses its focus that it formats the numbers to something like 1234.560 even if 1234.56 was typed in?   I want it to be formatted to 3 digits. 123 becomes 123.000, 123.4 becomes 123.400 etc. Does the above regular expressions accept only digits?

          R Offline
          R Offline
          RyanMorris
          wrote on last edited by
          #4

          Hi Brendan,

          .NET Enthusiast wrote:

          What's wrong with 1234.234?

          Sorry, I think I slightly misunderstood your question, I thought you wanted 3 digits each side of the decimal place. If that's not the case then you might want to try this instead: ^[-+]?\d+(\.\d{3})?$ - this will allow you to have as many numbers as you like in front of the decimal place but only 3 after it. To be honest I wouldn't have a clue how to reformat the number after losing focus on the text box, my javascript isn't very good. Sorry. All the expressions I've given you only except numbers. Thanks, Ryan

          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