isValidPhone
Validates whether a string is a well-formed E.164 international phone number. Requires a leading +, a non-zero country code, and 1–15 total digits. Common readability separators — spaces, hyphens, parentheses, and dots — are stripped before validation; any other character causes rejection.
Try it
Section titled “Try it”Import
Section titled “Import”import { isValidPhone } from "1o1-utils";import { isValidPhone } from "1o1-utils/is-valid-phone";Signature
Section titled “Signature”function isValidPhone({ phone, country }: IsValidPhoneParams): booleanParameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
phone | unknown | Yes | The value to validate |
country | CountryCode | No | ISO 3166-1 alpha-2 country code. When set, enforces the dialing prefix and total digit length for that country. ~50 popular countries supported. Default: omitted |
Returns
Section titled “Returns”boolean — true if the value is a parseable E.164 phone number, false otherwise.
Examples
Section titled “Examples”isValidPhone({ phone: "+5511999999999" }); // => trueisValidPhone({ phone: "+1 555 1234567" }); // => trueisValidPhone({ phone: "+44 (20) 7946-0958" }); // => trueisValidPhone({ phone: "+1.555.123.4567" }); // => true
isValidPhone({ phone: "11999999999" }); // => false (no country code)isValidPhone({ phone: "+0123456789" }); // => false (country code starts with 0)isValidPhone({ phone: "abc" }); // => falseisValidPhone({ phone: "" }); // => falseisValidPhone({ phone: null }); // => false// Country-scoped validationisValidPhone({ phone: "+5511999999999", country: "BR" }); // => trueisValidPhone({ phone: "+551133334444", country: "BR" }); // => true (BR landline)isValidPhone({ phone: "+15551234567", country: "BR" }); // => false (US dial code)isValidPhone({ phone: "+5511999", country: "BR" }); // => false (too short for BR)isValidPhone({ phone: "+15551234567", country: "US" }); // => trueSupported countries
Section titled “Supported countries”All ISO 3166-1 alpha-2 codes with an assigned ITU-T E.164 country code (~240 territories). Includes every UN member state plus dependent territories such as GG (Guernsey), IM (Isle of Man), JE (Jersey), BL (Saint Barthélemy), MF (Saint Martin), XK (Kosovo), BQ/CW/SX (former Netherlands Antilles), and minor Pacific islands (NU, TK, WF, etc.).
Passing a country outside this set returns false.
// Validate user input before sendingfunction notify(input: string) { if (!isValidPhone({ phone: input })) { throw new Error("Invalid phone"); } return sms.send(input);}Edge Cases
Section titled “Edge Cases”- Non-string inputs (
null,undefined, numbers, objects, arrays) returnfalse. - Empty strings return
falsewithout invoking the regex. - Inputs longer than 32 characters are rejected immediately to avoid pathological regex work.
- The leading
+is mandatory — local-format numbers without a country code returnfalse. - Country code cannot start with
0(E.164 rule). - Total digit count must be 1–15 after separators are stripped; 16+ digits return
false. - Allowed separators: space, hyphen, parentheses, dot. Any other character (
/,_, letters, etc.) causes rejection. - Country validation checks the dialing prefix and total digit count only — it does not distinguish mobile vs fixed-line, nor verify area codes.
- Countries that share a dialing prefix cannot be distinguished: passing
country: "CA"accepts US numbers and vice-versa (NANP, +1);country: "RU"accepts numbers from any other +7 country (e.g. Kazakhstan). - Validation is structural — it does not check whether the country code, area code, or subscriber number actually exist. For carrier-grade validation, use a library backed by phone metadata (e.g.
libphonenumber-js).
Also known as
Section titled “Also known as”phone, validate phone, is phone, valid phone, phone number, e164, international phone
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use isValidPhone to validate a user-supplied phone number before submitting a form.