Laravel

How to Validate Empty Space or White Space in Laravel 9 Input Fields?

Share your learning

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.

Satpal

Recent Posts

How to Switch PHP Versions in XAMPP Easily: Managing Multiple PHP Versions on Ubuntu

Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…

1 year ago

How to Use Coding to Improve Your Website’s SEO Ranking?

Let's understand about how to use coding to improve your website's SEO. In today’s computerized…

1 year ago

Most Important Linux Commands for Web Developers

Let's understand the most important linux commands for web developers. Linux, as an open-source and…

1 year ago

Top 75+ Laravel Interview Questions Asked by Top MNCs

Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…

1 year ago

Mailtrap Integration for Email Testing with Laravel 10

Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…

1 year ago

Firebase Cloud Messaging (FCM) with Ionic 6: Push Notifications

Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…

1 year ago