Validate and Format Phone Numbers in Zoho Creator with Deluge

Format Phone Numbers as (678) 800-1234 in Zoho Creator

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.

Before you start

One single-line field to validate — Phone_additional in this example. Rename it in both scripts to match your form. Nothing else is required.

Part 1 — the validation script

// 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);
}

Part 2 — the reusable function

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);
}

Notes

  • Why the function re-strips the digits. It is defensive: called from elsewhere with raw input, it still behaves. A helper that only works when its caller has already cleaned the data is a helper that breaks the first time someone else uses it.
  • It returns the input untouched on a bad length rather than throwing. A formatting helper should not be able to take down a form.
  • On Validate, not On Success. On Validate can still cancel the save. By On Success the record is already written.
  • International numbers. This is US and Canada only by design. For anything global, do not pattern-match — use a validation service, such as the Neutrino script elsewhere in this section.

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 →