Use Word Captcha Code and Validation in Laravel

Published on Apr 26, 2022

Hello Artisans, today I'll show how we can integrate captcha code and validation in our Laravel Application. For that, we will use captcha-com/laravel-captcha. Let's see how we can use it in our application.

Note: Tested on Laravel 9.2.

Table of Contents

  1. Install and setup captcha-com/laravel-captcha
  2. Create and Setup Controller
  3. Define Routes
  4. Create and Setup blade File
  5. Output

Install and setup captcha-com/laravel-captcha

Fire the below command in terminal to install laravel captcha package.

composer require mews/captcha

Now we'll register service provider and alias in config/app.php.

'providers' => [

'aliases' => [
	'Captcha' => Mews\Captcha\Facades\Captcha::class

Create and Setup Controller

Now we'll create a controller where we'll write our logic to show the validation messages and for refresh the captcha code. So, fire the below command in the terminal

php artisan make:controller HomeController

Now put the below code in your HomeController.


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
    public function myCaptcha()
        return view('captcha');

    public function myCaptchaPost(Request $request)
            'email' => 'required|email',
            'password' => 'required',
            'captcha' => 'required|captcha'
            ['captcha.captcha' => 'Invalid captcha code.']);
        dd("You are here :) .");

    public function refreshCaptcha(): \Illuminate\Http\JsonResponse
        return response()->json(['captcha'=> captcha_img()]);


Define Routes

Now we will define routes for refresh our captcha code and post a request form. Put these below routes in your web.php.

Route::get('my-captcha', [\App\Http\Controllers\HomeController::class,'myCaptcha'])->name('myCaptcha');
Route::post('my-captcha', [\App\Http\Controllers\HomeController::class,'myCaptchaPost'])->name('');
Route::get('refresh_captcha', [\App\Http\Controllers\HomeController::class,'refreshCaptcha'])->name('refresh_captcha');

Create and Setup blade File

Now first we'll create a blade file called captcha.blade.php where we'll put our form and show the validation messages. So, create a file under the resources/views/captcha.blade.php and put the below codes.

<html lang="en">
    <title>How to create captcha code in Laravel 9?</title>
    <link rel="stylesheet" href="">
    <link href="" rel="stylesheet"
          integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
    <script src=""></script>
<div class="container" style="margin-top: 50px">
    <div class="col-md-8 col-md-offset-2">
        <div class="panel panel-default">
            <div class="panel-heading">Login</div>
            <div class="panel-body">
                <form class="form-horizontal" method="POST" action="{{ route('') }}">@csrf
                    <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                        <label for="email" class="col-md-4 control-label">E-Mail Address</label>
                        <div class="col-md-6">
                            <input id="email" type="text" class="form-control" name="email" value="{{ old('email') }}">
                            @if ($errors->has('email'))
                                <span class="help-block">
                                  <strong>{{ $errors->first('email') }}</strong>
                    <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                        <label for="password" class="col-md-4 control-label">Password</label>
                        <div class="col-md-6">
                            <input id="password" type="password" class="form-control" name="password">
                            @if ($errors->has('password'))
                                <span class="help-block">
                                  <strong>{{ $errors->first('password') }}</strong>
                    <div class="form-group{{ $errors->has('captcha') ? ' has-error' : '' }}">
                        <label for="password" class="col-md-4 control-label">Captcha</label>
                        <div class="col-md-6">
                            <div class="captcha">
                                <span>{!! captcha_img() !!}</span>
                                <button type="button" class="btn btn-success btn-refresh"><i class="fa fa-refresh"></i>
                            <input id="captcha" type="text" class="form-control" placeholder="Enter Captcha"
                            @if ($errors->has('captcha'))
                                <span class="help-block">
                                  <strong>{{ $errors->first('captcha') }}</strong>
                    <div class="form-group">
                        <div class="col-md-8 col-md-offset-4">
                            <button type="submit" class="btn btn-primary">
<script type="text/javascript">
    $(".btn-refresh").click(function () {
            type: 'GET',
            url: '/refresh_captcha',
            success: function (data) {
                $(".captcha span").html(data.captcha);


Now we ready with our setup. It's time to check our output. If everything goes well you'll find a below output

Captcha Validation in Laravel 9

That's it for today. Hope you'll enjoy this tutorial. You can also download this tutorial from GitHub. Thanks for reading. 🙂

Comments (0)