Australian Mobile Validation CakePHP 3.x

This is a very simple…

Login

Blog History

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 *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.