johnny233 Posted June 30, 2022 Share Posted June 30, 2022 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? Quote Link to comment Share on other sites More sharing options...
Phi11W Posted June 30, 2022 Share Posted June 30, 2022 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. Quote Link to comment Share on other sites More sharing options...
maxxd Posted June 30, 2022 Share Posted June 30, 2022 You could also make life easier on yourself using crypto.randomUUID() Quote Link to comment Share on other sites More sharing options...
Barand Posted June 30, 2022 Share Posted June 30, 2022 Alternatively $str = 'FSOWPDHJEOSKJ3D2DF5R3FG51'; echo join('-', str_split($str, 5)); //--> FSOWP-DHJEO-SKJ3D-2DF5R-3FG51 1 Quote Link to comment Share on other sites More sharing options...
johnny233 Posted July 1, 2022 Author Share Posted July 1, 2022 (edited) 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 July 1, 2022 by johnny233 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.