Reputation:
i want a regular expression for length limit 10 digit with 2 decimal place numbers with only numbers allowed.10 digit before decimal
allowed
1
1111111111
111
1111111111.22
.2
1.2
1.22
Not allowed
4.
.
-1
abc
Null
Emptystring
""
1.222
-1.22
111111111111
tried but not working
^[0-9]*(\.[0-9]{0,2})?$
Upvotes: 0
Views: 2495
Reputation: 370679
You're almost there - all you have to do is include a check that the string is not empty (which can be accomplished with a positive lookahead for .{1,10}
right after the ^
), and check that its first digit string has at most 10 characters (just use a {0,10}
quantifier for the digits). Also note that [0-9]
simplifies to \d
.
In order to also exclude trailing dots, repeat the digit after the dot with {1,2}
instead of {0,2}
:
^(?!$)\d{0,10}(?:\.\d{1,2})?$
https://regex101.com/r/Ah8dNu/5
Upvotes: 2
Reputation: 550
I have also prepared below RegEx.
Seems this will also work.
^[0-9]{1,10}((\.)[0-9]{0,2}){0,1}$
Upvotes: 0