Skip to content
Advertisement

How to validate phone number in laravel 5.2? [closed]

I want to validate user input phone number where number should be exactly 11 and started with 01 and value field should be number only. How do I do it using Laravel validation?

Here is my controller:

JavaScript

Advertisement

Answer

One possible solution would to use regex.

JavaScript

This will check the input starts with 01 and is followed by 9 numbers. By using regex you don’t need the numeric or size validation rules.

If you want to reuse this validation method else where, it would be a good idea to create your own validation rule for validating phone numbers.

Docs: Custom Validation

In your AppServiceProvider‘s boot method:

JavaScript

This will allow you to use the phone_number validation rule anywhere in your application, so your form validation could be:

JavaScript

In your validator extension you could also check if the $value is numeric and 11 characters long.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement