ItSolutionStuff.com

Space Not Allowed Validation in Laravel Example

By Hardik Savani • April 16, 2024
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, laravel 8, laravel 9, laravel 10 and laravel 11 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...

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 7 Ajax Form Validation Example

Read Now →

Laravel 7 Form Validation Tutorial

Read Now →

Laravel Validation for Multiple Files in Array Example

Read Now →

How to use Google ReCaptcha in Laravel?

Read Now →

How to Create Custom Validation Rule in Laravel 9?

Read Now →

Laravel Ajax Image Upload with Validation Example

Read Now →

Bootstrap Form Validation using Validator.js Example

Read Now →

How to Add CKEditor Required Field Validation in JQuery?

Read Now →

Laravel File Upload with Validation Example

Read Now →

Laravel Client Side Validation using Parsley.js Example

Read Now →