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?
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?
if (/^\\d+\\.\\d{2}$/.test($('#amount').val())) {
// correct format
}
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 +.
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 …
/^\\d+(\\.\\d{2})?$/
in case anyone else was interested.