How to Allow Only Numbers Validation in Laravel ?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Today now in this blog,I will explain you how to validation only numeric in laravel application. we can easy to use validation number in laravel application . Here we are show the laravel numeric validation. So now here we will field under validation that’s allow only numbers in laravel application.

So in this example,we can simply apply to validation to Numeric Validation in laravel application. Now the field under validation so must have a numeric value.

So now here the example of laravel validation allow only numbers.

So let’s start and see below the solution

solution
$request->validate([
	"playerName" => "required",
	"score" => "required|numeric"
]);

Route : routes/web.php

Route::get('blogs','BlogController@index');
Route::post('blogs','BlogController@store')->name('blogs.store');

Controller : app/Http/Controllers/BlogController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Notifications\Notification;

class BlogController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('index');
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
      $request->validate([
        "playerName" => "required",
        "score" => "required|numeric"
      ]);
      return redirect()->back();
    }
}

View : resources/views/index.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Validation Allow Only Numbers - codingspoint.com</title>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.css">
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body class="bg-dark">
<div class="container">
    <div class="row">
        <div class="col-md-8 mt-4 offset-md-2">
            <div class="card">
                <div class="card-header">
                    <h5>Laravel Validation Allow Only Numbers - codingspoint.com</h5>
                </div>
                <div class="card-body">
                    <form accept="{{ route('blogs.store') }}" method="post">
                    @csrf
                        @if(count($errors) > 0)
                            <div class="alert alert-danger">
                                @foreach($errors->all() as $error)
                                        {{ $error }}<br>
                                @endforeach
                            </div>
                        @endif
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                <label>Player Name:</label>
                                 <input type="text" name="playerName" value="{{ old('playerName')}}" class="form-control">
                                </div>
                            </div>    
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                <label>score:</label>
                                 <input type="text" name="score" class="form-control" value="{{ old('score')}}">
                                </div>
                            </div>    
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <button class="btn btn-success" type="submit">Submit</button>
                            </div>
                        </div>
                    </form>
                   </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>    

Read Also : Laravel 8 Eloquent Global Scope Tutorial Example

I hope it will help you. Also you can follow us on Facebook

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →