Reputation: 3
How to validate these conditions in javacript?
Correct values samples:123 , 123.1, 123.55.
Upvotes: 0
Views: 68
Reputation: 14361
Use RegExp
/^(\d{3})(\.\d{1,2})?$/.test(number+'');
\d{3})
means any digits, exactly 3 times
(\.\d{1,2})?
is more complicated. It means after the three digits, there can be: A decimal, a digit 1-2 times
string
and a number
Upvotes: 1
Reputation: 2532
Regex: /^\d{3}(?:.\d{1,2})?$/
var pattern = /^\d{3}(?:.\d{1,2})?$/;
pattern.test(number);
Upvotes: 0