Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
confirm password validation in Laravel

Password and confirm password validation in Laravel

Aman Jain, January 26, 2022November 10, 2023

There is many ways to check the password and confirm password validation in laravel. we can match password using the equals to operator but in that case we need to work as core PHP.

So In laravel we have Validation library to create the validation rules. Laravel itself provides multiple way to check or if two inputs are same or not.

In this tutorial i will show you to check password and confirm password validation in laravel using various ways.

Below is the rules which we can use while checking password confirm validation

  1. Same ( The given field must match the field under validation )
  2. Confirmed (The field under validation must have a matching field of {field}_confirmation)

Or we can also create our own rule to match the validation.

Syntax for confirmed:

<?php 
use Illuminate\Support\Facades\Validator;

$validator = Validator::make($request, [
    'name' => 'required|min:3|max:50', 
    'password' => 'required|confirmed|min:6', // this will check password_confirmation 
                                              //field in request
]);

//OR example 2

$validator = Validator::make($request, [
    'name' => 'required|min:3|max:50', 
    'password' => 'required|min:6', 
    'password_confirmation' => 'required|same:password|min:6', // this will check password                           
]);

Here we used confirmed validation rule which will find password_confirmation field in request and it will check for the equality.

And other example we used same:password rule to check password_confirmation field with password.

Let’s understand Password and confirm password validation with example

Step 1: Create Laravel project

First step is to create the Laravel 8 project using composer command or you can also read the How to install laravel 8 ? and Laravel artisan command to generate controllers, Model, Components and Migrations

composer create-project laravel/laravel example-app

Step 2: Create controller

Now, create the controller and add the necessary imports and class. You can create by Laravel artisan or manually.

php artisan make:controller PostController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class PostController extends Controller
{

     /**
     * Show the form to create a new blog post.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        return view('post.create');
    }

    /**
     * Store a new blog post.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => 'required|email',   // required and email format validation
            'phone_number' => 'required|numeric|digits_between:9,15', // required and number field validation
            'username' => ['required'],
            'password' => 'required|confirmed',
            //OR
            // 'password_confirmation' => 'required|same:password',

        ]); // create the validations
        if ($validator->fails())   //check all validations are fine, if not then redirect and show error messages
        {
            
            return back()->withInput()->withErrors($validator);
            
        }
        else
        {
            return response()->json(["status"=>true,"message"=>"Form submitted successfully"]);
        }  
    }
     
}

Here , we added two methods one for to show the form view(create) and store to handle the form submit and validation.

Next, we have added validation for password and confirm password using confirmed rule.

Step 3: Create blade file for view

Now it’s time to show the form and validation message to our view, so let’s create the view file.

 <h3>Password and confirm password validation in laravel - Readerstacks</h3>

            <form method="post" id="validateajax" action="{{url('submit-post')}}" name="registerform">
              <div class="form-group">
                <label>Email</label>
                <input type="text" name="email" value="{{ old('email') }}" class="@error('email') is-invalid @enderror form-control" />

                @error('email')
                <div class="alert alert-danger">{{ $message }}</div>
                @enderror
                @csrf
              </div>
              <div class="form-group">
                <label>Phone number</label>
                <input type="text" name="phone_number" value="{{ old('phone_number') }}" class="@error('phone_number') is-invalid @enderror form-control" />
                @error('phone_number')
                <div class="alert alert-danger">{{ $message }}</div>
                @enderror
              </div>
              <div class="form-group">
                <label>Username</label>
                <input type="text" name="username" value="{{ old('username') }}" class="@error('username') is-invalid @enderror form-control" />
                @error('username')
                <div class="alert alert-danger">{{ $message }}</div>
                @enderror
              </div>
               <div class="form-group">
                <label>Password</label>
                <input type="password" name="password"  class="@error('password') is-invalid @enderror form-control" />
                @error('password')
                <div class="alert alert-danger">{{ $message }}</div>
                @enderror
              </div>
               <div class="form-group">
                <label>Confirm Password</label>
                <input type="password" name="password_confirmation"  class="@error('password_confirmation') is-invalid @enderror form-control" />
                @error('password_confirmation')
                <div class="alert alert-danger">{{ $message }}</div>
                @enderror
              </div>

              <div class="form-group">
                <button class="btn btn-primary">Register</button>
              </div>
            </form>

Step 4: Create routes

Now, open the routes/web.php and add below routes

<?php

use Illuminate\Support\Facades\Route;
use \App\Http\Controllers\PostController;

Route::get('/create-post',[PostController::class, 'create']);
Route::post('/submit-post',[PostController::class, 'store']);

Screenshot :

Screenshot 2022 01 26 at 7.24.09 PM
Password and confirm password  match laravel

Password and confirm password match laravel

Password and confirm password laravel

Also Read : Laravel ajax login and registration with example

Related

Uncategorized confirm passwordlaravelphp

Post navigation

Previous post
Next post

Related Posts

Laravel Call Controller Method from Another Controller in Laravel

How to Call Controller Method from Another Controller in Laravel ?

December 2, 2023March 16, 2024

In this article we will learn Call Controller Method from Another Controller in Laravel. Sometimes we need to access the controller method from another controller to save the same code on multiple locations. Best way to achieve it is using create a separate service class or trait in PHP so…

Read More

What is httpd.conf and httpd-vhost.conf file in apache?

August 28, 2021August 28, 2021

https.conf is main file of apache web server to handle the requests. Apache httpd.conf file generally located at /etc/httpd/conf/httpd, /etc/apache2/ , /etc/apache2/sites-enabled/ in ubuntu. httpd.conf contains the information of server root and port used routing. httpd-vhost contains additional information of virtual host.

Read More
Uncategorized call api in angular

How to call api in angular 12?

September 10, 2021November 7, 2023

In this tutorial, we are going to call the rest API’s using the angular HttpClient. To fetch data from server we need to call api in angular so we can get the updated data from serve. Angular has vast features and library to call the web or rest API’s. Step…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version