License key generation

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?

Hi @johnny230,

a very simple solution whould be the following:

let code = 'H3CJPE5JMS501FH52A';
code.match(/(.{1,5})/g).join('-'); // H3CJP-E5JMS-501FH-52A

So for a 18 character string you will have 3x5 Keys and 1x3 Keys.

Sponsor our Newsletter | Privacy Policy | Terms of Service