DEV Community

Cover image for Password and Confirm Password Validation in Laravel
Suresh Ramani
Suresh Ramani

Posted on • Originally published at techvblogs.com

6 1

Password and Confirm Password Validation in Laravel

Introduction

Laravel form requests are special classes that extend the functionality of regular request classes, enabling advanced validation features. Form requests also help to keep your controller actions a lot cleaner, because you can move all your validation logic to the form request class. Another benefit is that it allows you to filter requests before they reach your controller actions.

It’s always a good idea to put a password confirmation field in your forms since the password is a masked field and the user would never know if they made a typing mistake while filling out the password.

On the backend validation, you just need to use the confirmed validation rule for your password field.



$request->validate([
    'password' => 'required|confirmed|min:6'
]);


Enter fullscreen mode Exit fullscreen mode

Also in your HTML, you need to make sure the password field used to input confirmation from the user must be named password_confirmation.

Bootstrap FrontEnd Code for Laravel Password and Confirm Password Validation

Here is the bootstrap form code that includes both the fields and also displays an error when the validation fails.

Password and Confirm Password Validation in Laravel - TechvBlogs



<div class="form-group row">
    <label for="password" class="col-md-4 col-form-label text-md-right">Password</label>

    <div class="col-md-6">
        <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password">

        @error('password')
            <span class="invalid-feedback" role="alert">
                <strong>{{ $message }}</strong>
            </span>
        @enderror
    </div>
</div>

<div class="form-group row">
    <label for="password" class="col-md-4 col-form-label text-md-right">Confirm Password</label>

    <div class="col-md-6">
        <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password_confirmation" required autocomplete="current-password">
    </div>
</div>


Enter fullscreen mode Exit fullscreen mode

Thank you for reading this blog.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Cloudinary image

Video API: manage, encode, and optimize for any device, channel or network condition. Deliver branded video experiences in minutes and get deep engagement insights.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay