2
I am trying to create a national document validator that accepts numbers and letters or only number with 11 characters limit in Javascript.
The problem is in the creation of regex.
Follows the code:
var documento = 'abc123-$/';
alert(documento.replace(/^[A-Za-z0-9]{0,5}\d+[A-Za-z0-9]{0,6}$g/,"")); // resultado deveria ser: abc123
var documento = 'abcdef-$/';
alert(documento.replace(/^[A-Za-z0-9]{0,5}\d+[A-Za-z0-9]{0,6}$g/,"")); // resultado deveria ser vazio porque deve conter pelo menos 1 número
Follow an example in Jsfiddle: Jsfiddle
In the case of documents, I think it would be better to do the calculation of the digit(s) (s) verifier(s), that they usually have (find out what is the calculation of the document you are working on and implement it). Although regex "works", not every 11-digit/letter string will be a valid document if it has checker digits - and in this case the calculation of these already ensures that the document number is valid, making regex unnecessary.
– hkotsubo
That’s kind of impossible because I’m using a field for all the national documents. It is a system of sale of tickets online, it is necessary only that the user fill out a document with photo, can be from work card to AOB card in the same field.
– Bruno Folle