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