Users type phone numbers however they like — dots, dashes, spaces, a leading 1, parentheses. That variety makes numbers impossible to search, deduplicate, or hand to an API. This pair of scripts normalises every number to (678) 800-1234 on save, and rejects anything that is not ten digits.
Tidied up from the original. Three changes. The original called getAlphaNumeric(), which keeps letters as well as digits — so 555-CALL-NOW passed the length check and produced nonsense; this uses replaceAll("[^0-9]",""). It raised an alert but did not stop the save, so bad data still landed; cancel submit is added. And the reusable function referred to its own argument as input.phonefield, which is legacy syntax — current Creator addresses function arguments by bare name. An empty else block was dropped, and a leading country code is now tolerated rather than rejected.
One single-line field to validate — Phone_additional in this example. Rename it in both scripts to match your form. Nothing else is required.
// Trigger: On Add / On Edit > On Validate
if(!isBlank(input.Phone_additional))
{
digits = input.Phone_additional.replaceAll("[^0-9]","");
// Tolerate a leading US country code
if(digits.length() == 11 && digits.startsWith("1"))
{
digits = digits.subString(1,11);
}
if(digits.length() != 10)
{
alert "Phone number must be 10 digits. Received " + digits.length() + ".";
cancel submit;
}
input.Phone_additional = thisapp.Update.PhoneFormat(digits);
}Create this as a standalone function so any form in the app can call it. Keep the name Update.PhoneFormat and the script above works unchanged.
string Update.PhoneFormat(string phonefield)
{
digits = phonefield.replaceAll("[^0-9]","");
if(digits.length() != 10)
{
return phonefield; // hand back untouched rather than throw
}
return "(" + digits.subString(0,3) + ") " + digits.subString(3,6) + "-" + digits.subString(6,10);
}This script is part of the free Creator Scripts Deluge Library.
All 39 Deluge scripts, the full Zoho Creator course, and every downloadable asset are now free. Get free access →