Restrict some characters in auto-generate licence key

I restrict Some characters to generate licence key .
following characters
0
1
5
S
U
V

my coding is

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>product-key</title>

   <body>
<script language="javascript" type="text/javascript">

function generateUUID() {
   var d = new Date().getTime();

var nochar=/^[OUVS]/;
   var uuid = 'Oxxx-xxxx-xxxx-xxxx'.replace(/[x]/g, function(c) {
       var r = (d + Math.random()*36)%36 | 0;

         d = Math.floor(d/36);


       return (c=='x' ? r : (r&0x7|0x8)).toString(36);
         });




   var title = document.getElementById('UUID');
   title.innerHTML = uuid.toUpperCase();

}


</script>
<h1 id="UUID"></h1>
<input type="submit" id="byBtn" value="Generate UUID" onclick="generateUUID()"/>
</body>
</html>

any possible way to restrict above mentioned character

So your goal is to generate these UUIDs, but none of the x’s can be any of the following characters?

0, 1, 5, S, U, V

Yes.I am a new programmer. please give your suggestion

I recommend generating an array of the possibly valid items, then choosing random elements from it. For example:

const randomElement = (arr) => arr[Math.floor(Math.random() * arr.length)];
const chars = "ABCDEFGHJKLMNPQRTWXYZ2346789".split('')

Now randomElement(chars) will return one randomly selected letter from chars. Use that in your .replace method and you’re all set. For example:

> "xxxxx-xxxxx-xxxxx".replace(/x/g, (_) => randomElement(chars))
'Z9F9G-N92YL-BC2ET'

(You might also want to remove ‘Z’, ‘2’, ‘O’, and ‘Q’ since they’re easily confusable as well)

1 Like

Seems like @chuckadams’ solution is simple and meets the requirements of the problem.

@chuckadams @RandellDawson Thanks for your suggestions.