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.

$args = array( 'Country Code' => '971' );
echo format_mobile_as_international("+971555555555", $args);

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.

$contacts = array( 'John' => '055 5555555',
	'Anna' => '+966123456789',
	'Phil' => '(966)512345678',
	'BAD' => '055555555', // will be omitted
	'Maha' => '055.000.0000'
foreach ( $contacts as $name => $number ) {
	$mobile = format_mobile_as_international( $number );
	if ( $mobile ) {
		echo '<p><a href="' . $mobile . '" target="_blank">Send WhatsApp Message to ' . $name . '</a></p>';



Add Yours →

Leave a Reply