ItSolutionStuff.com

Laravel Validation Allow Only Numbers Example

By Hardik Savani • April 16, 2024
Laravel

Here, i will show you how to works laravel validation for number only. you can see laravel validation allow only numbers. This article will give you simple example of allow only numbers in laravel validation. We will use allow only number in laravel validation. So, let's follow few step to create example of laravel numeric validation example.

we will use numeric validation rule for allow only numbers in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.

we may sometime requirement to add validation for allow only number in our laravel application. so i will show how to validation for accept only numbers in laravel 7 using laravel alpha. you can easily use with your controller method.

I will give you way to add validation in laravel. so i will just show you controller code and preview here. so you can also follow form validation with laravel with this code: Laravel Form Validation Example.

Example 1:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

use Illuminate\Validation\Rule;

class StoreUser extends FormRequest

{

/**

* Determine if the user is authorized to make this request.

*

* @return bool

*/

public function authorize()

{

return true;

}

/**

* Get the validation rules that apply to the request.

*

* @return array

*/

public function rules()

{

return [

'name' => 'required',

'phone' => 'required|numeric'

];

}

}

Example 2: digits_between Add Digits Minimum 3 and Maximum 5

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

use Illuminate\Validation\Rule;

class StoreUser extends FormRequest

{

/**

* Determine if the user is authorized to make this request.

*

* @return bool

*/

public function authorize()

{

return true;

}

/**

* Get the validation rules that apply to the request.

*

* @return array

*/

public function rules()

{

return [

'name' => 'required',

'amount' => 'required|digits_between:3,5'

];

}

}

Example 3: Add Digits Minimum 3 and Maximum 5

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

use Illuminate\Validation\Rule;

class StoreUser extends FormRequest

{

/**

* Determine if the user is authorized to make this request.

*

* @return bool

*/

public function authorize()

{

return true;

}

/**

* Get the validation rules that apply to the request.

*

* @return array

*/

public function rules()

{

return [

'name' => 'required',

'amount' => 'required|numeric|min:3|max:5'

];

}

}

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Unique Validation on Multiple Columns Example

Read Now →

Laravel Unique Validation With Soft Delete Example

Read Now →

Laravel Unique Validation on Update Example

Read Now →

How to Use Unique Validation in Laravel?

Read Now →

Special Characters Not Allowed Validation in Laravel

Read Now →

Laravel Form Validation Request Class Example

Read Now →

Space Not Allowed Validation in Laravel Example

Read Now →

Laravel Mobile/Phone Number Validation Example

Read Now →

Laravel Bail Rule | Stop Validation On First Failure

Read Now →

Laravel Validation for Multiple Files in Array Example

Read Now →