Space Not Allowed Validation in Laravel Example
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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 7 Ajax Form Validation Example
- Laravel 7 Form Validation Tutorial
- Laravel Validation for Multiple Files in Array Example
- How to use Google ReCaptcha in Laravel?
- How to Create Custom Validation Rule in Laravel 9?
- Laravel Ajax Image Upload with Validation Example
- Bootstrap Form Validation using Validator.js Example
- How to Add CKEditor Required Field Validation in JQuery?
- Laravel File Upload with Validation Example
- Laravel Client Side Validation using Parsley.js Example