Format (xxx) xxx-xxxx
var oField = event.srcElement;
if (typeof(oField) != "undefined" && oField != null && oField.DataValue != null)
{
var sTmp = oField.DataValue.replace(/[^0-9]/g, "");
if(sTmp.length == 10)
{
oField.DataValue = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
}
else
{
alert('Phone must contain 10 numbers.');
}
}
Format xxx-xxx-xxxx
var oField = event.srcElement;
if (typeof(oField) != "undefined" && oField != null && oField.DataValue != null)
{
var sTmp = oField.DataValue.replace(/[^0-9]/g, "");
if(sTmp.length == 10)
{
oField.DataValue = sTmp.substr(0, 3) + "-" + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
}
else
{
alert('Phone must contain 10 numbers.');
}
}
Format xxx.xxx.xxxx
var oField = event.srcElement;
if (typeof(oField) != "undefined" && oField != null && oField.DataValue != null)
{
var sTmp = oField.DataValue.replace(/[^0-9]/g, "");
if (sTmp.length == 10)
{
oField.DataValue = sTmp.substr(0, 3) + "." + sTmp.substr(3, 3) + "." + sTmp.substr(6, 4);
}
else
{
alert('Phone must contain 10 numbers.');
}
}
Other Formatting Issues:
Remove leading digit if number is 1
var sTmp = oField.DataValue.replace(/[^0-9]/g, "");
if (sTmp.substr(0, 1) == "1")
{
sTmp = sTmp.substr(1, sTmp.length-1);
}
Allowing only 7 digits (no area code)
else if (sTmp.length == 7)
{
oField.DataValue = sTmp.substr(0, 3) + "-" + sTmp.substr(3, 4);
}
else
{
alert('Phone must contain 7 or 10 numbers.');
}
Allowing extensions (requires area code)
else if (sTmp.length > 10)
{
oField.DataValue = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4) + " ext: " + sTmp.substr(10, sTmp.length-10);
}
else
{
alert('Invalid phone number. If using an extension, you must enter area code also.');
}
No comments:
Post a Comment