How to Validate Empty Space or White Space in Laravel 9 Input Fields?
In web development, data validation is a crucial aspect of ensuring data accuracy and preventing data loss.
Laravel, one of the most popular PHP frameworks. It provides robust validation tools that help to validate data before it is processed. One of the common validation requirements is to validate input fields for empty space or white space.
In this tutorial, we will explore how to validate empty space or white space in Laravel input fields.
Why need to validate empty space or white space in Laravel input fields?
Before we dive into the Laravel validation process, it is essential to understand why this validation is crucial. In most cases, users may unintentionally enter empty spaces or white spaces when filling out a form. If this data is not validated, it may lead to incorrect data storage, processing, and retrieval. Hence, it can ultimately affect the application’s functionality.
Therefore, it is vital to validate empty space or white space to ensure that the data entered is accurate and reliable.
Validate Empty Space or White Space with regular expression
Laravel provides a straightforward way to validate empty space or white space in input fields using the ‘required’ validation rule. The ‘required’ validation rule checks whether the input field has any content, including empty space or white space. Here is an example of how to validate empty space or white space in Laravel input fields:
public function store(Request $request) { $request->validate([ 'username' => 'required|regex:/^\S*$/u', 'password' => 'required|regex:/^\S*$/u', ]); // Store data in database }
In the above example, the ‘username’ and ‘password’ input fields are validated using the ‘required’ validation rule and the ‘regex’ validation rule. The ‘regex’ rule checks whether the input field contains any whitespace characters using the '\S'
regex pattern. If the input field has any whitespace characters, the validation fails, and the user is prompted to enter the correct data.
Create custom rule to validate white space or empty space
Laravel has the feature to generate custom validation rules classes. Run the following command to generate a new rule class.
php artisan make:rule EmptySpace --invokable
It will generate the new validation rule class in the app/Rules
directory. Now, you can see your custom rule class like below. It will have a magic method __invoke
and it returns the error message if validation fails.
<?php namespace App\Rules; use Illuminate\Contracts\Validation\InvokableRule; class EmptySpace implements InvokableRule { /** * Run the validation rule. * * @param string $attribute * @param mixed $value * @param \Closure $fail * @return void */ public function __invoke($attribute, $value, $fail) { if ( preg_match('/^\S*$/u', $value) == false ) { $fail('The :attribute must not include empty spaces.'); } } }
After creating this custom white or empty space validation rule, you can use this rule class in the controller or where you are validating the input request.
Here is an example of implementation:
use App\Rules\EmptySpace; $request->validate([ 'name' => ['required', 'string', new EmptySpace], ]);
Conclusion
In conclusion, validating empty space or white space in Laravel input fields is a crucial aspect of data validation. Laravel provides an easy and straightforward way to validate input fields using the ‘required’ validation rule and the ‘regex’ validation rule. By following the steps outlined in this tutorial, you can ensure that the data entered in your application is accurate and reliable.
We hope that this tutorial has been informative and useful in helping you validate empty space or white space in Laravel input fields.