Australian Mobile Validation CakePHP 3.x

Written by James McDonald

August 27, 2018

This is a very simple mobile validation for Australian Mobiles. It probably would also benefit from having all the symbols except + stripped too

// src/Model/Table/Users.php where Users is the class name
// of the table field your are validating
$australianMobile = '/^(0|\+61)4\d{8}$/';

$validator
      ->add('mobile_number', 'custom', [
                'rule' => function ($value, $context) use ($australianMobile) {
                    // remove spaces to make the regex simpler
                    $check = preg_replace('/\s/', '', $value);
                    
                    // checks for either of these styles
                    // +61412 345 678 or 0412 345 678
                    $found = preg_match($australianMobile, $check);
                    return boolval($found);
                },
                'message' => 'Please enter a valid mobile number.'
            ]);

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

You May Also Like…

How to Research a CPU Upgrade

How to Research a CPU Upgrade

Upgrade Time! Doing a lot of VMWare Workstation virtualization to create labs for self-study and training. Finding...