php - How to validate phone number in laravel 5.2? -
i want validate user input phone number number should 11 , started 01 , value field should number only. how do using laravel validation?
here controller:
public function saveuser(request $request){ $this->validate($request,[ 'name' => 'required|max:120', 'email' => 'required|email|unique:users', 'phone' => 'required|min:11|numeric', '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'); }
one possible solution use regex.
'phone' => 'required|regex:/(01)[0-9]{9}/'
this check input starts 01 , followed 9 numbers. using regex don't need numeric
or size
validation rules.
if want reuse validation method else where, idea create own validation rule validating phone numbers.
in appserviceprovider
's boot
method:
validator::extend('phone_number', function($attribute, $value, $parameters) { return substr($value, 0, 2) == '01'; });
this allow use phone_number
validation rule anywhere in application, form validation be:
'phone' => 'required|numeric|phone_number|size:11'
in validator extension check if $value
numeric , 11 characters long.
Comments
Post a Comment