Estoy tratando de validar una entrada en Angular que solo puede tener un máximo de 14 dígitos y un máximo de 2 decimales (separados por comas)
El caso es que Debuggex está validando mi expresión como correcta pero en Angular no funciona:
^[0-9]{0,14}(,?)[0-9]{1,2}$Validador angular:
Validators.pattern(/^[0-9]{0,14}(,?)[0-9]{1,2}$/)Lo que necesito:
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)¡Tanques!
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}`; }Prueba esto:
Validators.pattern(/^[0-9]{1,14}(,[0-9]{1,2})?$/)Eso hace que la coma que incluye la parte decimal sea opcional.