Space Not Allowed Validation in Laravel Example

By Hardik Savani July 7, 2020 Category : Laravel

In this tutorial we will go over the demonstration of laravel validation not allow spaces. i explained simply about laravel validation username example. We will look at example of space not allowed validation in laravel. it's simple example of laravel validation not allow whitespace. You just need to some step to done laravel validation without spaces.

we may sometime requirement to add now allow whitespace validation in our laravel application. so i will show how to add validation for no allow space validation in laravel 6, laravel 7 and laravel 8 using regex. we will create custom validation for no space allow in laravel.

I will give you a way to add no allow space 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.

You can see bellow preview:

Preview:

Example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

use Validator;

class HomeController extends Controller

{

/**

* Show the application dashboard.

*

* @return \Illuminate\Http\Response

*/

public function create()

{

return view('createUser');

}

/**

* Show the application dashboard.

*

* @return \Illuminate\Http\Response

*/

public function store(Request $request)

{

Validator::extend('without_spaces', function($attr, $value){

return preg_match('/^\S*$/u', $value);

});

$request->validate([

'name' => 'required',

'username' => 'required|without_spaces',

'email' => 'required|email|unique:users'

],

[

'username.without_spaces' => 'Whitespace not allowed.'

]);

$input = $request->all();

$user = User::create($input);

return back()->with('success', 'User created successfully.');

}

}

I hope it can help you...