Raj Bhatt
How do I Create a GUID Number Using JavaScript?
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>
  6. Guid Number
  7. </title>
  8. </head>
  9. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  10. <body>
  11. <button type="button" id="btnclick">Click Me!</button>
  12. <script type="text/javascript">
  13. function uuidv4() {
  14. return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
  15. (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
  16. );
  17. }
  18. console.log(uuidv4());
  19. $('#btnclick').click(function(){
  20. alert(uuidv4());
  21. });
  22. </script>
  23. </body>
  24. </html>
By Raj Bhatt in JQuery on Feb 05 2023
  • Manoj Kumar Duraisamy
    Feb, 2023 7

    function generateGUID() {
    return ‘xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx’.replace(/[xy]/g, function(c) {
    var r = Math.random() * 16 | 0, v = c == ‘x’ ? r : (r & 0x3 | 0x8);
    return v.toString(16);
    });
    }
    ```

    This function generates a string in the format xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, where x and y are hexadecimal digits generated randomly. The function replaces each x or y with a randomly generated digit using the Math.random() method and the toString(16) method to convert the result to hexadecimal.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS