This JScript sample script shows how to use an onChange event to format a phone number as it is entered.
This script attempts to format a phone number with between seven and ten digits using standard United States formatting.
// Get the field that fired the event .var oField = event.srcElement;
// Verify that the field exists and is not null.
if (typeof(oField) != "undefined" && oField != null)
{
// Remove any non-numeric characters.var sTmp = oField.DataValue.replace(/[^0-9]/g, "");
// If the number is an expected length and is supported, format the number.
switch (sTmp.length)
{
case "4105551212".length:
oField.DataValue = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
break;
case "5551212".length:
oField.DataValue = sTmp.substr(0, 3) + "-" + sTmp.substr(3, 4);
break;
}
}