JavaScript examples: validate and clean data
Check and clean values in Run Custom Code: required values, emails, phone numbers, URLs, ZIP codes, ranges and JSON, stopping with fail().
- Verified Nov 17, 2026
- Classic builder
On this page
- Before you start
- Stop the step, or report the problem
- Require values
- Check an email address
- Clean up a US phone number
- Turn a phone number into international (E.164) form
- Accept only values from a list
- Check a web address
- Check a US ZIP code
- Check that a number is in a range
- Check a card number for typing mistakes
- Block throwaway email domains
- Read JSON text safely
- Trim and collapse spaces
Snippets for a Run Custom Code step that check values and clean them up: required values, email addresses, phone numbers, URLs, ZIP codes, number ranges and JSON. For how the step works, see Run custom JavaScript in a workflow.
Before you start #
- Add the step Add a Run Custom Code step to your workflow and leave Language set to JavaScript.
- Add the inputs Under Data to use in the code, click + Add Input once for each input the snippet lists. Type the name from the snippet in Variable name and choose where the value comes from, for example Record Value (Source) or Custom Value.
- Paste and test Click the Code box to open the code editor, paste the snippet, and click Run. The Response tab shows what the code returned.
- Use the results In a later step, open the value picker, choose Step Data, pick this step, and pick the key the snippet returned (for example
full_name).
An input is available in the code as $ followed by its variable name: an input named first_name is $first_name. Add every input a snippet uses; a name with no matching input stops the code with a "not defined" error.
Stop the step, or report the problem #
A snippet can handle a bad value in one of two ways:
- Stop the step with
fail("message"). The step ends with an error, and the message appears in the run log. Later steps still run, unless you turn on Abort workflow if this step fails on the step’s Advanced tab. - Report and carry on by returning a flag and a message, for example
returnData("valid", false)andreturnData("error", "..."). A Condition or Branches step can then decide what happens next.
// Inputs: email
if (!String($email ?? "").includes("@")) {
fail("The email address is missing or not valid.");
}
returnData("email", String($email).trim().toLowerCase()); After a step stops with fail, a later Condition step can also check that step’s Status or Error.
Require values #
// Inputs: first_name, email, company
const values = { first_name: $first_name, email: $email, company: $company };
const missing = Object.keys(values).filter(k => String(values[k] ?? "").trim() === "");
if (missing.length) {
fail("Missing required values: " + missing.join(", "));
}
returnData("message", "All required values are present"); Check an email address #
A format check only: it cannot tell whether the mailbox exists.
// Input: email
const email = String($email ?? "").trim().toLowerCase();
const valid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
returnData("valid", valid);
returnData("email", valid ? email : "");
returnData("error", valid ? "" : "Invalid email format"); Clean up a US phone number #
// Input: phone "(555) 123-4567" -> "+15551234567"
let digits = String($phone ?? "").replace(/\D/g, "");
if (digits.length === 11 && digits.startsWith("1")) digits = digits.slice(1);
const valid = digits.length === 10;
returnData("valid", valid);
returnData("phone_e164", valid ? "+1" + digits : "");
returnData("phone_pretty", valid ? `(${digits.slice(0, 3)}) ${digits.slice(3, 6)}-${digits.slice(6)}` : ""); Turn a phone number into international (E.164) form #
A rough length check (10 to 15 digits), not a full check of each country’s numbering rules.
// Inputs: phone, country_code (Custom Value: "1" for the US, "44" for the UK, ...)
const raw = String($phone ?? "").trim();
const country = String($country_code ?? "1");
let digits = raw.replace(/\D/g, "");
if (raw.startsWith("+")) {
// already has a country code
} else if (raw.startsWith("00")) {
digits = digits.slice(2);
} else {
digits = country + digits.replace(/^0+/, "");
}
const valid = digits.length >= 10 && digits.length <= 15;
returnData("valid", valid);
returnData("phone_e164", valid ? "+" + digits : ""); Accept only values from a list #
// Input: status
const ALLOWED = ["draft", "submitted", "approved", "rejected"];
const status = String($status ?? "").trim().toLowerCase();
const known = ALLOWED.includes(status);
returnData("status", known ? status : "draft");
returnData("was_changed", !known); Check a web address #
Accepts http:// and https:// addresses with a dot in the host name.
// Input: url
const url = String($url ?? "").trim();
const valid = /^https?:\/\/[^\s/?#]+\.[^\s/?#]+([/?#]\S*)?$/i.test(url);
returnData("valid", valid);
returnData("url", valid ? url : ""); Check a US ZIP code #
// Input: zip
const zip = String($zip ?? "").trim();
const valid = /^\d{5}(-\d{4})?$/.test(zip);
returnData("valid", valid);
returnData("zip5", valid ? zip.slice(0, 5) : ""); Check that a number is in a range #
// Inputs: value, min, max
const v = Number($value);
const min = Number($min);
const max = Number($max);
if (String($value ?? "").trim() === "" || Number.isNaN(v)) {
fail("The value is not a number.");
}
if (v < min || v > max) {
fail(`The value ${v} is outside ${min} to ${max}.`);
}
returnData("value", v); Check a card number for typing mistakes #
The Luhn checksum catches most typing mistakes. It does not tell you whether the card is real or valid.
// Input: card_number Checks the number was typed correctly. Never return or store the full number.
const digits = String($card_number ?? "").replace(/\D/g, "");
let sum = 0, double = false;
for (let i = digits.length - 1; i >= 0; i--) {
let n = Number(digits[i]);
if (double) { n *= 2; if (n > 9) n -= 9; }
sum += n;
double = !double;
}
returnData("valid", digits.length >= 13 && digits.length <= 19 && sum % 10 === 0);
returnData("last4", digits.slice(-4)); Block throwaway email domains #
// Input: email
const BLOCKED = ["mailinator.com", "tempmail.com", "guerrillamail.com", "10minutemail.com"];
const domain = String($email ?? "").trim().toLowerCase().split("@")[1] || "";
if (BLOCKED.includes(domain)) {
fail("Please use a permanent email address.");
}
returnData("domain", domain); Read JSON text safely #
// Input: raw_json (for example a webhook body or a Long Text field)
let data;
try {
data = JSON.parse(String($raw_json ?? ""));
} catch (e) {
fail("raw_json is not valid JSON: " + e.message);
}
returnData("data", data); Trim and collapse spaces #
Return the cleaned values, then save them with an Update Record step: in its value picker choose Step Data and this step.
// Inputs: first_name, last_name, company
const clean = v => String(v ?? "").replace(/\s+/g, " ").trim();
returnData("first_name", clean($first_name));
returnData("last_name", clean($last_name));
returnData("company", clean($company)); Was this page helpful?
Something wrong or missing on this page? Tell us.