I have a text field with id=amount.
How can I use JQuery to check that it only contains the digit 1234567890 and one and only one "." symbol?
| SitePoint Sponsor |



I have a text field with id=amount.
How can I use JQuery to check that it only contains the digit 1234567890 and one and only one "." symbol?

Note that the way I've set it up, it only allows two numbers after the dot. If you want it to be any number, replace the {2} with a +.Code:if (/^\d+\.\d{2}$/.test($('#amount').val())) { // correct format }



I tested it with all the scenarios I can think of and it works perfect. Thanks.



I know in my specification I mentioned I wanted at least one decimal and then the code given is correct.
However, in the event that if someone wanted the decimal portion to be optional then one can do ...
in case anyone else was interested.Code:/^\d+(\.\d{2})?$/
Bookmarks