How do you generate an UUID in JavaScript?
Since Crypto API is standard in all modern browsers, it’s as simple as invoking a method from the crypto
object:
crypto.randomUUID();
And validating it is just a matter of specifying a regular expression and calling the test()
method on it.
let regex = /^[a-z,0-9,-]{36,36}$/;
let uuid = crypto.randomUUID();
/* Will return true if the regex is valid */
regex.test(uuid);