How to validate phone numbers in a website form

Let people correct the selected country, and validate the number again on your server before saving. At Theetasoft, we treat country context as part of the input. A number pasted without a calling code can be hard to interpret until you know where it belongs.

Give the form enough context

Use a phone field with type="tel" and a country selector for local numbers. When someone enters a number starting with +, use the calling code they entered. Keep their input in the form when a check fails so they can fix a digit or choose a different country.

Check the format with JavaScript

Use the browser check for quick feedback. On submission, run the same validation rules on the server before storing the result.

JavaScript example using awesome-phonenumber
import { parsePhoneNumber } from 'awesome-phonenumber';

function checkNumber(input, country) {
  const value = input.trim();
  if (!value.startsWith('+') && !country) {
    return { error: 'Choose a country or enter the number with its + calling code.' };
  }

  const result = value.startsWith('+')
    ? parsePhoneNumber(value)
    : parsePhoneNumber(value, { regionCode: country });

  return result.valid
    ? { number: result.number.e164 }
    : { error: 'Check the selected country and look for a missing digit.' };
}

Pass the country as a two-letter region code, such as GB for the United Kingdom. If your form accepts extensions, give them a separate field and store them alongside the normalized number.

Keep format and verification separate

Store the normalized number and verification status in separate fields. A format check tells you that the number fits a numbering plan. If your workflow needs to confirm access to the line, record that outcome after a separate verification step.