Create serial number generator button

Asked

Viewed 827 times

-1

So, I need to make a button that generates a serial, inside an input text when clicking. But the serial would need to contain [wf][2017] as the default and the remaining random. Ex: wf2017892143.

I think it’s with JS. But some idea of how to make this button generating this random S/N?

3 answers

10

Here is a suggestion. At least generate wf20171000, and all Ids are unique.

You could use a more complex ID with uuid, which would be numeric alpha, but for what you described I think the example below works.

var geraNumeroUnico = (function() {
  var saidos = [];
  return function() {
    var numero = Math.round(Math.random() * 1e8) + 1000;
    if (saidos.includes(numero)) return geraNumeroUnico();
    saidos.push(numero);
    return numero;
  }
})();


function geraId() {
  return 'wf2017' + geraNumeroUnico();
}

// teste

for (var i = 0; i < 100; i++) {
  console.log(i, geraId());
}

  • 1

    Show man! That’s what I need! But how do I get it into a certain input!?

  • @Jaidermasterinfo input.value = geraId();

  • 1

    Top guy! Excellent tip! Thanks a lot!

0

HTML/Javascript Solution:

<html>
    <head>
        <script>

            function gerar_string_aleatoria( tam, charset )
            {
                var serial = "";

                for( var i = 0; i < tam; i++ )
                    serial += charset.charAt( Math.floor(Math.random() * charset.length) );

                return serial;
            }


            function gerar_serial()
            {
                return 'wf2017' + gerar_string_aleatoria( 6, "0123456789" );
            }

        </script>
    </head>

    <body>
        <form name='frm'>
            <input type='text' name='txtSerial' value=''>
            <input type='button' name='btnGerarSerial' onClick='document.frm.txtSerial.value = gerar_serial();'>
        </form>
    </body>

</html>

-1


The html code:

<input type="text" value="" id="txt_serial">
<input type="button" value="gerar" id="botao_serial">

Import the jquery:

<scritp src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>

The code using jquery:

function geraSerial() {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < 6; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return "wf2017" + text;
}
$(function(){
    $('#botao_serial').click(function(){
        $('#txt_serial').val(geraSerial());
  });
});

Upshot: https://jsfiddle.net/wictor/uqs57ugL/1/

  • Very nice guy! Thank you!

  • @Jaidermasterinfo just out of curiosity: you marked this answer as accepted but you said before how much I asked you: "would just be numbers". You changed your mind? :)

  • @Sergio true, I have missed this point, but just change the variable 'possible' by putting only the numbers: var possible = "0123456789";

  • @Wictorchaves in this case your answer gives me between 10 to 20 duplicates in every 5000 generated: https://jsfiddle.net/Sergio_fiddle/qv651Lp2/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.