Australian Mobile Validation CakePHP 3.x

by | Aug 27, 2018 | IT Tips | 0 comments

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 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.