SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru laflair13's Avatar
    Join Date
    Nov 2004
    Location
    TN
    Posts
    629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trying to get a set of characters using javascript.

    Hope I explain this right.


    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>
    Thanks In Advance

    David

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Something like this should do it:
    Code:
    function makeid(text){
        if(text == undefined)
            var text = '';
            var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            for( var i=0; i < 5; i++ ){
                text += possible.charAt(Math.floor(Math.random() * possible.length));
            }
        
        return text.length < 29 ? makeid(text+"-") :  text;
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •