How to validate phone number with dial code Laravel 8 in best way?

Input validation is essential for all applications. When we are creating a Laravel application, it is important to assure that we validate user input properly. Laravel framework has a wide set of validation rules. However, at times the framework may not support certain types of validations. In these cases we can create validation methods ourselves. Let’s us learn about the various methods we can use to validate phone number with dial code.

We can add the validate function in the save data method of the Controller Class.

Validate phone number with Laravel supported rules

In the save function we can use the validate method provided by the Illuminate\Http\Request object. A phone number is validated in the steps below.

  • On validation rule pass, code keeps executing normally.
  • On validation rule fails, due to an invalid phone number, an Illuminate\Validation\ValidationException exception will be thrown and the error response is automatically sent back to the user.

If validation fails for a traditional HTTP request, it generates a redirect response to the previous URL. If the incoming request is an XHR request, it returns a JSON response with the validation error messages. 


 public function store(Request $request)
   {
     $validated = $request->validate([
     phone_number=> 'required|numeric|min:12'
     ]);

     // On validation pass, method will continue here.
   }

Rule Breakdown of Phone Validation

The validation rule: ‘required|numeric|min:10’ is used for phone validation. Let’s see what each of these mean. Here are the definitions for each of them.

  • numeric: The input must be numeric, i.e. a number.
  • min:12: This field must be at least 12 characters in length.
  • required: The field under validation must be present and not null or empty.

A field is considered “empty” or “null” if any one of the following conditions are true.

  • The value is an empty string.
  • The value is null.
  • The value is an uploaded file with no path.
  • The value is an empty array or empty Countable object.

So the user must enter a numeric value that has to be 12 characters in length. This validation method used in the Controller class ensures the user input phone number is validated correctly. 

However, you can also use Regex pattern within the Laravel framework to add phone number validation to your application. Let’s demonstrate this!

Validate phone number using regex pattern

If you decide to use Regex to validate phone numbers, you don’t have to use the digits or numeric rules. While using Regex, you may find many various patterns that well suit your use case. Let’s use a simple save method that stores telephone numbers, and apply a regular expression to act as our custom made validation rule.

Consider the following code and validation example where we use a function with the same name as our previous example:

public function store(Request $request){
  $validated = $request->validate($request,[
  'phone_number' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:12'
  ]);
  // store data method continues here.
}

The rule break down to our phone number input is as follows.

  • required: this rule validates against empty strings, null, empty arrays, or uploaded files with no paths. 
  • regex:/^([0-9\s\-\+\(\)]*)$/
  • The regex pattern accepts number 0-9, and the additional characters of ‘-‘,’+’,'(‘, and ‘)’. The validate method rejects any input that does not match this given pattern. 
  • min:12: This input must be at least 12 characters in length. The validate method rejects any input that does not follow these size validation rules. 

Both validation methods are good to be used. However, when it comes to validate phone number with dial code. The regex pattern method is more beneficial. As it allows us to create a customized method ourselves. The regex pattern can have any pattern we put in. So, we can add dial code validation in the pattern and validate phone number with dial code.

Leave a Comment