Laravel Regex Strong Password Validation Tutorial Example


Share:
Published By: Admin, Published On: May 19, 2021, Category: Laravel

Hi Dev,

Today, I will give you how to use strong password validation example with regex in laravel we can create strong password validation before registration in laravel.

In this tutorial password validation example we will force utilizer to give password must be more than 8 characters long, should contain at-least 1 Uppercase, 1 Lowercase, 1 Numeric and 1 special character before submit the form.

Now, This is is going to be a very simple tutorial on how to utilize regex validation to make your password strength requisite more stronger for a better security. We don't utilize any package to make stronger password. We will utilize regex validation to make your password stronger requisite more stronger.

So, With regex strong password laravel we will additionally check password strong validation in laravel. Now you can optically discern the preview of password validation in laravel of this tutorial example.

Here, I will show full example of regex strong password validation in laravel follow step by step.

Step 1: Install Laravel 8

In first step to create multi page form validation n laravel , if you haven't laravel 8 application setup then we have to get fresh laravel 8 application. So run bellow command and get clean fresh laravel 8 application.

composer create-project --prefer-dist laravel/laravel blog
Step 2: Add Route

Open your routes/web.php and paste the following code.

routes/web.php
<?php

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

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/


Route::get('/home', [RegexpassController::class ,"index"])-> name("regex.show");
Route::post('pass/store', [RegexpassController::class ,"store"])-> name("regex.store");

Step 3: Create Controller

In this point, now we should create new controller as RegexpassController. this controller will manage layout and getting data request and return response, so put bellow content in controller file:

app/Http/Controllers/RegexpassController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class RegexpassController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        return view('welcome');

    }

    /**
     * Write code on Method
     *
     * @return response()
     */

    public function store(Request $request)
    {
        $request->validate([
        'name' => 'required', 'string', 'max:255',
        'email' => 'required', 'string', 'email', 'max:255', 'unique:users',
        'password' => 'required|string|min:6|confirmed|regex:/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{6,}$/',
     ]);

     return redirect()->back();
    }
}

Step 4: Create Blade File

Now, In this step we need to create blade file to create regex strong password laravel 8. So create welcome.blade.php file.

resources/views/welcome.blade.php
    
@extends('layouts.app')
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
@section('content')
<div class="container bg-light">
    <div class="row justify-content-center">
        <div class="col-md-10">
            <h3 class="my-3">Regex Strong Password Validation Tutorial In Laravel - MyWebTuts.com</h3>
            <div class="card">
                <div class="card-header bg-secondary text-white">{{ __('Register') }}</div>

                <div class="card-body">
                    <form method="POST" action="{{ route('regex.store') }}">
                        @csrf

                        <div class="form-group row">
                            <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>

                            <div class="col-md-6">
                                <input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" autocomplete="name" autofocus>

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

                        <div class="form-group row">
                            <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" autocomplete="email">

                                @error('email')
                                    <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">{{ __('Password') }}</label>

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

                                @error('password')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ 'Your password must be more than 8 characters long, should contain at-least 1 Uppercase, 1 Lowercase, 1 Numeric and 1 special character.' }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>

                            <div class="col-md-6">
                                <input id="password-confirm" type="password" class="form-control" name="password_confirmation" autocomplete="new-password">
                            </div>
                        </div>

                        <div class="form-group row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    {{ __('Register') }}
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
 

I Hope It will help you..

Share: