Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel validation for a number which should be exactly 10 digits

Tags:

php

laravel

I am trying to validate a form which has phone number and we want that to be exactly 10 digits. But it gives errors even when the input is right I am using the following code in the controller so please advise as to what needs to be changed.

public function tytraining(Request $request)
    {

        $this->validate($request, [
            'txt_name'  => 'required',
            'txt_email' => 'required|email',
            'txt_phone' => 'required|numeric|between:9,11'
        ]);

        $name = Input::get('txt_name');

        $email = Input::get('txt_email');

        $phone = Input::get('txt_phone');

        $type = "bls-training";
        $url = '?' . Input::get('url');

        $medicalRequest = new ParseObject("MedicalRequest");

        $medicalRequest->set("name", $name);
        $medicalRequest->set("email", $email);
        $medicalRequest->set("phone", $phone);
        $medicalRequest->set("type", $type);
        $medicalRequest->set("requestMessage", "training");
        $medicalRequest->set("url", $url);

        try {
            $medicalRequest->save();

        } catch (ParseException $ex) {
            echo(' Some Error Occured');
        }
    }

Please advise how should I do the validation through Laravel..

Thank you all in advance for spending your valuable time and going through my concern and helping me out.

like image 402
Dhaval Chheda Avatar asked Dec 06 '16 05:12

Dhaval Chheda


People also ask

How can I validate my phone number in laravel 8?

Validate Phone Number Laravel public function save(Request $request) { $validated = $request->validate([ phone_number=> 'required|numeric|min:10' ]); //If number passes validation, method will continue here. } In our save function we use the validate method provided by the Illuminate\Http\Request object.

Is unique validation in laravel?

Laravel includes a wide variety of convenient validation rules that you may apply to data, even providing the ability to validate if values are unique in a given database table.


1 Answers

use size and digits validation https://laravel.com/docs/5.3/validation#rule-digits

$this->validate($request, [
            'txt_name'  => 'required',
            'txt_email' => 'required|email',
            'txt_phone' => 'required|digits:10'
        ]);
like image 111
Sagar Rabadiya Avatar answered Sep 19 '22 21:09

Sagar Rabadiya