.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Javascript >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Generate GUID from clientside using Javascript

Posted By:nishithraj       Posted Date: December 24, 2009    Points: 10    Category: Javascript    URL: http://www.dotnetspark.com  
 

Generating GUId from .NET is very easy process.

As it requires only one line of code we can generate it easily from server side.

But how about the guid generation is requierd from the clientside.

The below code will help you to achieve this

function GenerateGuidfrmJavascript() 
{ 
   try 
   { 
      var guid=GenerateGUID()+GenerateGUID()+"-"+GenerateGUID()+"-"+GenerateGUID()+"-"+GenerateGUID()+"-"+GenerateGUID()+GenerateGUID()+GenerateGUID(); 
      return ValidateGuid(guid); 
   } 
    
   catch (e) 
   { 
      var g = ""; 
      for(var i = 0; i < 32; i++) 
         g += Math.floor(Math.random() * 0xF).toString(0xF) + (i == 8 || i == 12 || i == 16 || i == 20 ? "-" : "") 
          
      return ValidateGuid(g) ; 
   } 
}

 

function GenerateGUID() 
{ 
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1); 
} 
  
function ValidateGuid(guid) 
{ 
   var exp=/^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$/; 
   if(exp.test(guid)) 
   { 
      return guid; 
   } 
   else 
   { 
      return GenerateGuidfrmJavascript();
   } 
}

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend