Jump to content

Recommended Posts

Hi, I have pre-written code that produces a license key but I don’t like the format that the key is in and was wondering if I could get some help fixing it?
This is the code

@section('scripts')
<script type="text/javascript">
    function Createkey(length) {
       var result           = '';
       var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
       var charactersLength = characters.length;
       for ( var i = 0; i < length; i++ ) {
          result += characters.charAt(Math.floor(Math.random() * charactersLength));
       }
       return result;
    }
    $('#licenses-gen').click(function(){
        var GetType = $('#type_of_key_id').find(":selected").text();
        $('#licence').val(GetType.trim()+Createkey(18));
    });
    </script>
@endsection

the output is just a random string of 18 characters like: H3CJPE5JMS501FH52A

though I want to get an output like: FSOWP-DHJEO-SKJ3D-2DF5R-3FG51

What can I change in the code to get the second output option?

Link to comment
https://forums.phpfreaks.com/topic/314977-license-key-generation-help/
Share on other sites

14 hours ago, Phi11W said:

Something like this? 

for ( var i = 0 ; i < length ; ++i ) {
   if ( ( 0 = ( i % 5 ) ) && ( 0 != i ) ) 
      result .= '-' ; 
   result .= characters.charAt(Math.floor(Math.random() * charactersLength));
}

Regards, 
   Phill  W. 

I was hopeful for this but it doesn't give me an output now, like no key is even generated now

Edited by johnny233
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.