Reputation: 7682
I need a RegEx pattern that will match on a valid decimal number string with two decimal places, or a malformed number only if the malformed number is an integer with a decimal place at the end.
This is a good discussion on achieving a match on two decimal places
Simple regular expression for a decimal with a precision of 2
But, I need these following strings to match:
Match:
Not Matched:
FYI: This is for masking an input field so that the user can enter any two digit number. The problem currently is that the field rejects the decimal place because it doesn't recognize it as a decimal number unless the user hits the left button to take the cursor back a character and then presses the decimal point key.
Upvotes: 2
Views: 1532
Reputation: 10360
Try this Regex:
^\d+(?:\.\d{0,2})?$
Explanation:
^
- start of the string\d+
- matches 1+ digits(?:\.\d{0,2})?
- matches a decimal followed by 0 to 2 digits. ?
makes this whole part optional so as to allow the Integers too.$
- End of the StringUpvotes: 2