Javacript Random Number Add Front Number


#1

I am using the following JavaScript to create a random confirmation number.

<script>
  function randomNumber(len) {
    var randomNumber;
    var n = '';

    for(var count = 0; count < len; count++) {
        randomNumber = Math.floor(Math.random() * 10);
        n += randomNumber.toString();
    }
    return n;
}

document.getElementById("confirm").value = randomNumber(6);

</script>

I would like to add a letter in front of my number confirmation. Example " B-[random number here]". However, when I try I kill my JavaScript.

Any help would be appropriated.


#2

Hi there mickeyatty1,

does this help...

<input id="confirm" type="text">

<script>

  function randomNumber(len) {
    var randomNumber;
    var n = '';
    var letters = [ 'A','B','C','D','E','F','G','H','I','J','K','M' ];
    var l = letters[ Math.floor(Math.random() * letters.length )];
    for( var count = 0; count < len; count ++ ) {
        randomNumber = Math.floor( Math.random() * 10 );
        n += randomNumber.toString();
    }
    n = l  + '-' +  n;
    return n;
 }

   document.getElementById( 'confirm' ).value = randomNumber(6);

</script>

coothead


#3

Thank you so much!


#4

 
 
          No problem, you're very welcome. :winky:
 
 
      coothead


#5

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.