How to make an automatic code generated with javascript? [closed]

0

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

    
asked by Bryant Medina 03.11.2016 в 03:42
source

1 answer

1

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 code largo , 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>
    
answered by 03.11.2016 в 04:02