I'm trying to validate an input in Angular that can only have a maximum of 14 digits and a maximum of 2 decimals (comma separated)
The thing is that Debuggex is validating my expression as correct but in Angular doesn't work:
^[0-9]{0,14}(,?)[0-9]{1,2}$
Angular validator:
Validators.pattern(/^[0-9]{0,14}(,?)[0-9]{1,2}$/)
What I need:
55555555555555,12 CORRECT (MAXIMUN 14 DIGITS AND MAXIMUM 2 DECIMALS)
55555555555555,1 CORRECT (MAXIMUM14 DIGITS AND 1 DECIMAL)
555555555555555,12 INCORRECT (15 DIGITS AND TWO DECIMALS)
5555555555, INCORRECT (COMMA WITHOUT DECIMALS)
Tanks!
html:
<html>
<body>
<input type="text" value="abcd1werty2gnv" id="in" placeholder="enter here"/>
<p id="txt"></p>
</body>
</html>
JS:
function foo() {
const s = document.getElementById("in").value;
const regex = /(\d)/g;
let c = s.match(regex).length;
let l = s.length;
document.getElementById("txt").textContent = `length: ${l} digits: ${c}`;
}
Try this:
Validators.pattern(/^[0-9]{1,14}(,[0-9]{1,2})?$/)
That makes the comma including the decimal part optional.