I need a code to appear in a text box generated by javascript that can be seen by the user, this code could be in the form: nota001 and incremented as: nota002 ... nota003 and so on, thanks for your help
I need a code to appear in a text box generated by javascript that can be seen by the user, this code could be in the form: nota001 and incremented as: nota002 ... nota003 and so on, thanks for your help
Here I propose a common code generator since it is not clear what would be the use of this código
.
- The function
generarCodigo
, generates a length codelargo
, value that is customizable.- The generated code is totally random and uses
mapa
to define the possible characters for this new code.
function generarCodigo(largo) {
var mapa = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
codigo = '',
i = 0;
for(i; i < largo; i++) {
codigo += mapa.charAt(Math.floor(Math.random() * mapa.length));
}
return codigo;
}
function imprimirCodigo() {
var largo = parseInt(document.getElementById('largo').value, 10) || 5;
document.getElementById('codigo').innerHTML = generarCodigo(largo);
}
Codigo: <span id="codigo"></span>
<br/>
<input id="largo" type="number" value="5" min="5" />
<button type="button" onclick="imprimirCodigo()">Generar</button>