Sunday, June 5, 2016

Phone Number Validation in Laravel | How to Validate Phone Number in Laravel?

request validation


In this tutorial I'm gonna show how you can validate user phone number using Laravel Validation Request. As we know usually we write the validation request in Controller. So Let's define a controller where we will show the validation rule.

Controller:



public function saveUser(Request $request){
        $this->validate($request,[
            'name' => 'required|max:120',
            'email' => 'required|email|unique:users',
            //'phone' => 'required|numeric|size:11'
            'phone' => 'required|regex:/(01)[0-9]{9}/'
            ]);

        $user = new User();
        $user->name=  $request->Input(['name']);
        $user->email=  $request->Input(['email']);
        $user->phone=  $request->Input(['phone']);
        $user->save();
       return redirect('success');     
    }

As we can see here I have used two validation rule which one you prefer use that one. 

//'phone' => 'required|numeric|size:11' 'phone' => 'required|regex:/(01)[0-9]{9}/'

First one I have used for checking phone number. As Phone number should be numeric I have used numeric and it should be 11.

In Second one I have used Regular Expression for validation. Where Phone number should be 11 digit where it starts with 01 and following 9 digit would as regard from any any digit from 0 to 9.

Here I have shown  two process  of Phone number validation. You can also create your own validation rule.

That's it for today. Stay connected for the next session.
Thank you!

Related Articles

4 comments:

  1. hi, nice tutorial ,what does the {9} mean in your validation?

    ReplyDelete
    Replies
    1. That specified the number of times the preceding statement should be repeated.

      Delete

  2. I do trust all the ideas you have introduced to your post.
    Free Email Validator

    ReplyDelete