Laravel Password Validation Example

Laravel 6 , Laravel

Nicesnippets

55

05-02-2020


Hi Dev,

In this blog, we will learn you password validation in laravel application. Laravel in password validation like regex string, min , max, required etc. we will regex password verification using custom validation rule in laravel application.

Regex validation use to make your password strength requirement stronger for a better security. You will implement Strong Password Regex Validation with Laravel Application.

Min use to user enter minmum character and Max to user enter maximum character. I will give you full example for laravel password valdation.

Let's see example and you can use anyone as you need.

Solution : 1

public function passwordValidation(Request $request)

{

$request->validate([

'password' => 'required|string|min:6|max:10|confirmed|

regex:/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{6,}$/',

]);

}

Solution : 2

public function passwordValidation(Request $request)

{

$request->validate([

'password' => ['required','string','min:10','regex:/[a-z]/',

'regex:/[A-Z]/',

'regex:/[a-z]/',

'regex:/[0-9]/',

'regex:/[@$!%*#?&]/'],

]);

}

Example

Route : routes/web.php

Route::get('password-validation','PasswordValidationController@passwordValidation')->name('password.index');

Route::post('password-validation','PasswordValidationController@storePasswordValidation')->name('password.store');

Controller : app/Http/controllers/PasswordValidationController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PasswordValidationController extends Controller

{

public function passwordValidation()

{

return view('validation.password');

}

public function storePasswordValidation(Request $request)

{

$request->validate([

'password' => ['required','string','min:10','regex:/[a-z]/',

'regex:/[A-Z]/',

'regex:/[a-z]/',

'regex:/[0-9]/',

'regex:/[@$!%*#?&]/'],

]);

return redirect()->back();

}

View : resources/views/validation/password.blade.php

<!DOCTYPE html>

<html>

<head>

<title>laravel password validation example - nicesnippets.com</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha256-L/W5Wfqfa0sdBNIKN9cG6QA5F2qx4qICmU2VgLruv9Y=" crossorigin="anonymous" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha256-WqU1JavFxSAMcLP2WIOI+GB2zWmShMI82mTpLDcqFUg=" crossorigin="anonymous"></script>

</head>

<body>

<div class="container">

<div class="row">

<div class="col-md-6 offset-3">

<div class="card mt-5">

<div class="card-header bg-success">

<h3 class="text-white text-center"><strong>Laravel Password Validation - NiceSnippets.com</strong></h3>

</div>

<div class="card-body">

@if(count($errors) > 0)

@foreach($errors->all() as $error)

<div class="alert alert-danger">{{ $error }}</div>

@endforeach

@endif

<form action="{{ route('password.store') }}" method="post">

@csrf

<div class="form-group">

<label><b>Password :-</b></label>

<input type="password" name="password" class="form-control" value="{{ old('password') }}">

</div>

<div class="form-group text-center">

<button class="btn btn-success" type="submit">Save</button>

</div>

</form>

</div>

</div>

</div>

</div>

</div>

</body>

</html>

It will help you...


Recommended Posts