JavaScript Serial Number Generator

To generate a random serial number, based on a given mask, you can use the following code:

// Serial Number Generator
// Generates a random number in a certain interval
function GenerateRandomNumber(min,max)
{
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

// Generates a random alphanumberic character
function GenerateRandomChar() {
	var chars = "1234567890ABCDEFGIJKLMNOPQRSTUVWXYZ";
	var randomNumber = GenerateRandomNumber(0,chars.length - 1);

	return chars[randomNumber];
}

// Generates a Serial Number, based on a certain mask
function GenerateSerialNumber(mask)
{
	var serialNumber = "";
	
	if(mask != null)
	{
		for(var i=0; i < mask.length; i++)
		{
			var maskChar = mask[i];
			
			serialNumber += maskChar == "0" ? GenerateRandomChar() : maskChar;
		}
	}
	
	return serialNumber;
}

// Generate a new Serial Number for a given mask
alert(GenerateSerialNumber("00000-00000-00000-00000-00000"));

2 comments

  1. Guys, how to use this code in CRM 2011 ?? i mean.. exactly we need to put this. Please explain with some example . Thanks in advance.

Leave a comment

Your email address will not be published. Required fields are marked *