Fill up Leading Zero(s) In JavaScript

When the user didn't input in the max length in the text box, leading zero(s) will
automatically be added.For example:Month Calculation : (123456789).If the user only selected "1", it hasn't reached the max length (01) yet, then a leading zero will be added to become "01"

function AddLeadingZero(currentField)
{

//Check if the value length hasn't reach its max length yet

if (currentField.value.length != currentField.maxLength)
{

//Add leading zero(s) in front of the value

var numToAdd = currentField.maxLength - currentField.value.length;
var value ="";
for (var i = 0; i < numToAdd;i++)
{
value += "0";
}
currentField.value = value + currentField.value;
}
}

Copy this function in the code file:
public static void AddLeadingZeroAttribute(TextBox txt)
{

//Add leading zero when focus is lost
txt.Attributes.Add("OnBlur", "AddLeadingZero(this)");

}

"OnBlur" event will be triggered when the textbox loses the focus. Whenever the user finished keying in values and left the textbox, the AddLeadingZero() function will be called.