Laravel Validation Allow Only Numbers

Laravel 6 , Laravel , Laravel 7

Nicesnippets

3249

20-01-2020


Laravel Validation Allow Only Numbers

Hii dev

Today,I will explain how to validation numeric in laravel 7/6. you can easy to use validation number in laravel 7/6 we are show the 7/6 numeric validation. we will field under validation allow only numbers

in laravel

In this example,you can simply apply to validation to Numeric Validation in laravel 7/6. The field under validation must have a numeric value.

here the example of laravel validation allow only numbers

let 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 - nicesnippets.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 - nicesnippets.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>

It will help you...


Recommended Posts