Reputation: 85
I've come up with this regular expression to validate a number which can have Maximum length-13 (including decimal points),Maximum no of decimal points-3,Maximum length of a whole number-12.
^(\d{1,12}([.]\d{1,1})?|\d{1,11}([.]\d{1,2})?|\d{1,10}([.]\d{1,3})?)$
Could anyone tell me if my approach is correct or give me a better solution?
Upvotes: 3
Views: 78
Reputation: 4603
This would also work:
^(?=.{1,13}$)(\d{1,12})(\.\d{1,3})?$
Uses positive look ahead to match the entire string length is ok. Then it uses a group to match from 1 - 12 digits Then there's an optional group to match a decimal followed by 1-3 digits.
Edited: Simplified since the rules don't allow a 13 digit integer-part
Upvotes: 3