I would need to know how to validate a input
form with regular expressions that must necessarily contain 6 numbers.
So far I have not succeeded, I leave the code.
if (document.formCliente.numeroContabilidad.value!="") {
var regNUM = /^ ([0-9]) *$/;
if (regNUM.test(document.formCliente.numeroContabilidad.value) == false){
alert("El numero de contabilidad es incorrecto.");
document.formCliente.numeroContabilidad.focus();
return false;
}
}
Start of text, a number repeated 6 times, end of text
Which is the same as:
And if we want to prevent it from starting with zero, we look for it to match a digit from 1 to 9, followed by 5 more digits:
Alternatively, in HTML5 you can leave the validation to the browser:
When using the attribute
pattern
, it is always anchored to^
y$
, so there is no need to add it to the regex.All modern browsers support it ( see table ).