I am trying to use a regular expression to validate decimal values . I wrote below regular expression but it does not allowing first decimal with value like a .5 or .6 or .1
Regular Exp : /^\d[0-9]{0,13}(\.\d{1,2})?$/
Rules :
- It should allow positive numbers.
- It should allow max 13 numbers before decimal point
- It should allow max two number after decimal.
- It should allow .(dot) with number like a .5
- It should not allow the .0
Example - Valid inputs
- 0
- 0.5
- 1.55
- .5
- 1234567890123(13 numbers before decimal)
- 1234567890123.5
- 1234567890123.00
Example - invalid inputs
- .(dot),
- .0
- 1.234
- 5.
- 12345678901234(14 numbers before decimal)
- 12345678901234.56
const valid = [
"0",
"0.5",
"1.55",
".5",
"1234567890123",
"1234567890123.5",
"1234567890123.00",
];
const invalid = [
".",
".0",
"1.234",
"5.",
"12345678901234",
"12345678901234.56",
];
const rgx = /^\d[0-9]{0,13}(\.\d{1,2})?$/
console.log("Checking valid strings (should all be true):");
valid.forEach(str => console.log(rgx.test(str), str));
console.log("\nChecking invalid strings (should all be false):");
invalid.forEach(str => console.log(rgx.test(str), str));