Validating Phone Numbers in Laravel 5.2: Beyond Basic Numeric Checks
In Laravel 5.2, handling user input validation is often more about understanding what the built-in rules actually do rather than just adding them to a list. You might encounter a specific requirement where a phone number must be exactly 11 characters long, start with 01, and contain only digits. While your initial controller logic attempts this using standard validators, relying solely on numeric or min:11 is insufficient for this specific constraint.
The rule numeric in Laravel 5.2 checks if the value is a number. However, it does not strictly enforce that the field contains only digits before decimal points; depending on how the data is cast, it might pass integer or float values. More critically, min:11 only ensures the length is greater than or equal to 11. It doesn't prevent a number like 012345678901 (12 digits) from passing, nor does it enforce the mandatory prefix.
To achieve the exact behavior you describedâexactly 11 characters, starting with 01, and containing numbers onlyâyou need to combine strict length checks with a regex pattern. In Laravel 5.2, the most straightforward way to enforce these constraints is by using the regex rule alongside required.
Here is how you can update your validation array in the saveUser method:
public function saveUser(Request $request){
$this->validate($request, [
'name' => 'required|max:120',
'email' => 'required|email|unique:users',
'phone' => 'required|size:11|regex:/^01[0-9]{9}$/',
'course_id' => 'required'
]);
$user = new User();
$user->name = $request->Input(['name']);
$user->email = $request->Input(['email']);
$user->phone = $request->Input(['phone']);
$user->date = date('Y-m-d');
$user->completed_status = '0';
$user->course_id = $request->Input(['course_id']);
$user->save();
return redirect('success');
}
The key here is replacing min:11|numeric with a combination of size:11 and the regex rule. The size:11 rule explicitly ensures the string length is exactly 11 characters, failing immediately if it is longer or shorter. This prevents users from entering extra spaces or accidental digits that might otherwise slip through a "min" check.
The regex pattern /^01[0-9]{9}$/ is constructed to match your specific requirements:
^asserts the start of the string.01matches the literal characters required at the beginning.[0-9]{9}ensures that exactly 9 digits follow the prefix, bringing the total length to 11.$asserts the end of the string.
By anchoring the pattern with ^ and $, you guarantee that no extra characters appear before or after the required sequence. This approach is significantly more robust than relying on numeric, which can be ambiguous regarding input types in certain contexts.
One common pitfall when working with phone numbers in older Laravel versions like 5.2 is the handling of whitespace. If a user submits their number as "01234 56789", the size rule will fail because spaces count toward the character count. While you could strip whitespace before saving, it is often better to enforce strict formatting at the validation stage to catch errors early. This ensures data integrity before the record even enters the database.
If you require a more complex validation setup or need to integrate these rules into a form builder, consider looking into resources like https://laravelcompany.com for additional patterns on handling input sanitization in Laravel applications.