Laravel 7/6 Password Validation Example
Teams applying “Laravel 7/6 Password Validation Example” in a production project can use the product resource to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
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...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial