Laravel Validation allow only Numbers Example

Laravel Validation allow only Numbers Example; In this tutorial you will learn how to use laravel validation to accept only number.

Many times we need to add validation in input fields for allow only number in our laravel application. So here we show how to validation for accept only numbers in laravel using laravel.

Example 1: Using Numeric

Laravel predefined numeric validation is accept only numeric values; So you can use like below;

public function store(Request $request)
{
    $request->validate([
        "name" => "required",
        "score" => "required|numeric"
    ]);
  
  return back();
}

Example 2: Add Digits Minimum 8 and Maximum 16 

This example show you how to use numeric with degits between;

public function store(Request $request)
{
    $request->validate([
        "name" => "required",
        "score" => "required|numeric|digits_between:8,16"
    ]);
  
  return back();
}

Example 3: Add Digits Minimum 8 and Maximum 16

We can use the min or max with numeric validation just like below;

public function store(Request $request)
{
    $request->validate([
        "name" => "required",
        "password" => "required|numeric|min:8|max:16"
    ]);
  
  return back();
}

Leave a Comment