1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Quick javascript help

Discussion in 'HTML & JavaScript' started by Oblivion13, Jan 4, 2013.

  1. Oblivion13

    Oblivion13 Regular Member

    Joined:
    Sep 7, 2011
    Messages:
    459
    Likes Received:
    247
    I have a script that does random numbers. It only does 5 numbers right now. (like - w4hty)

    I am trying to have it do numbers like this

    2jsnf-csjxm-cnjc2-fucns-cnash

    Like how it does 5 then - then 5 more. I need a total of 25 characters.

    Can anyone help me with this?

    Here is the code I have for getting just the 5

    Code:
     <script>
          function makeid(){
            var text = "";
            var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            for( var i=0; i < 5; i++ ){
              text += possible.charAt(Math.floor(Math.random() * possible.length));
            }
            return text;
          }
          
          document.getElementById("uniqueID").innerHTML = makeid();
        </script>
    
     
  2. ombrocapelo

    ombrocapelo Registered Member

    Joined:
    Aug 7, 2012
    Messages:
    67
    Likes Received:
    582
    Code:
    <script>
        function makeid(){
            var text = "";
            var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            for( var x=0; x < 5; x++ ){
                for( var i=0; i < 5; i++ ){
                    text += possible.charAt(Math.floor(Math.random() * possible.length));
                }
                if (x < 4){
                    text += "-";
                }
            }
            return text;
        }
        document.getElementById("uniqueID").innerHTML = makeid();
    </script>
    
    Output for example : HKmiH-VJkHt-jF3ES-04AUH-jx6lI
     
    • Thanks Thanks x 2
  3. Oblivion13

    Oblivion13 Regular Member

    Joined:
    Sep 7, 2011
    Messages:
    459
    Likes Received:
    247
    Thank You very much, Worked like a charm.
     
    • Thanks Thanks x 1
  4. james2012

    james2012 Newbie

    Joined:
    Mar 6, 2012
    Messages:
    25
    Likes Received:
    3
    Occupation:
    Webmaster
    Location:
    Miami, Florida
    Home Page:
    Hi ombrocapelo;

    It's a great logic which is the best idea to use it. What will you do? - To build such logic? Sometimes, it's more difficult to build such logic. Can you share some idea to build such quick logic?