Simple Mobile Number Validator

This function is meant to be used in a loop to prepare mobile phone numbers to be used as inputs where they are expected to be in the full international format without IDD Prefix (i.e. 00, 011, 0010, or +), for example 966555555555, but where the provided number has not necessarily been validated upon entry.

With the default settings (for Saudi Arabia), mobile phone numbers that will be accepted include:

  • Correct numbers as dialed locally with preceding zero:
    e.g. 0555555555
  • Correct numbers without preceding zero:
    e.g. 555555555 (perhaps coming from Excel)
  • Correct numbers already in international format with or without preceding plus sign or Saudi IDD:
    e.g. 00966555555555, 966555555555 or +966555555555

Invalid numbers will simply be omitted.

By overriding the defaults, it can be used for any country’s phone numbers. You can change the defaults directly in the function, or for more flexibility, pass an associative array as the second argument to override the defaults.

An example of how this might be used is to insert an appropriately formatted mobile phone number into a url for sending an SMS or WhatsApp message.


Add Yours →

Leave a Reply