Laravel Validate Form on Submit using Google reCaptcha Validation Example

Laravel Validate Form on Submit using Google reCaptcha Validation Example

Validate Form on Submit using Google reCaptcha Validation Example in Laravel

A Qoutes about using captcha is "Tough on bots and Easy on humans".You are going to use Google recaptcha so you must know the benefits of using Google recaptcha.

Nomally any captcha protect your websites from spamming.

Google day by day made changes in their library as in past you have to validate via awful distorted text which you have to put in the text box to verify that you are not a robot but now there is a single click to confirm that yes you are not a robot.

reCaptcha is a totally free service that make you websites away from being spam so it means reCaptcha is built only for security purpose.

I am going to use NoCaptcha reCaptcha which is improved version of Captcha announced by Google.

We all know that when Google announce improved version then definitely they will put some great benefits.

In Laravel, There are so many packages to use Google NoCaptcha reCaptcha.

Almost every websites using captcha for security reason either in contact form or in register form.

Let's start with anhskohbo/no-captcha packages that is very popular now a days. As I told you there are many packages are available like buzz/laravel-google-captcha and google/recaptcha you can use any one of them.

Step1: anhskohbo/no-captcha package installation

I will recommend Composer that is widely used as a dependecy manager for PHP packages. To add this dependency using the command, run following command from your project directory.

composer require anhskohbo/no-captcha

Now add the service provider to the providers array in following path config/app.php

config/app.php
Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class
Step2: Add NOCAPTCHA_SECRET and NOCAPTCHA_SITEKEY

Add these line in your .env file

NOCAPTCHA_SECRET=[secret-key] - 6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe
NOCAPTCHA_SITEKEY=[site-key] - 6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI

To get secret-key and site-key click here Recaptcha Admin

For now Google provide dummy Site key and Secret key. All verification requests will pass by using these keys in your website and No Catpcha will be display on your website but this will be for only testing purpose.

Step3: Add Route and Controller functions

  1. Route::get('captcha-form-validation',array('as'=>'google.get-recaptcha-validation-form','uses'=>'FileController@getCaptchaForm')) ;
  2. Route::post('captcha-form-validation',array('as'=>'google.post-recaptcha-validation','uses'=>'FileController@postCaptchaForm')) ;

Add these two methods in your controller.


  1. public function getCaptchaForm(){
  2. return view('files.captchaform');
  3. }
  4. public function postCaptchaForm(Request $request){
  5. $this->validate($request, [
  6. 'name' => 'required',
  7. 'email'=>'required|email',
  8. 'phone'=>'required|numeric|digits:10',
  9. 'details' => 'required',
  10. 'g-recaptcha-response' => 'required|captcha',
  11. ]);
  12. }
Step4: Add Blade File

Now create a blade file where you have to validate your form using Google no-captcha recaptcha

resources/views/files/captchaform.blade.php

  1. <div class="panel panel-primary">
  2. <div class="panel-heading">Google reCaptcha Validation Example</div>
  3. <div class="panel-body">
  4. <div class="row">
  5. <div class="col-xs-12 col-sm-12 col-md-6 col-md-offset-3">
  6. {!! Form::open(array('route' => 'google.post-recaptcha-validation','method'=>'POST','files'=>true,'id'=>'myform')) !!}
  7. <div class="col-xs-12 col-sm-12 col-md-12">
  8. <div class="form-group">
  9. <strong>Name:</strong>
  10. {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
  11. {!! $errors->first('name', '<p class="alert alert-danger">:message</p>') !!}
  12. </div>
  13. </div>
  14. <div class="col-xs-12 col-sm-12 col-md-12">
  15. <div class="form-group">
  16. <strong>Email:</strong>
  17. {!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!}
  18. {!! $errors->first('email', '<p class="alert alert-danger">:message</p>') !!}
  19. </div>
  20. </div>
  21. <div class="col-xs-12 col-sm-12 col-md-12">
  22. <div class="form-group">
  23. <strong>Phone:</strong>
  24. {!! Form::text('phone', null, array('placeholder' => 'Mobile No','class' => 'form-control')) !!}
  25. {!! $errors->first('phone', '<p class="alert alert-danger">:message</p>') !!}
  26. </div>
  27. </div>
  28. <div class="col-xs-12 col-sm-12 col-md-12">
  29. <div class="form-group">
  30. <strong>Details:</strong>
  31. {!! Form::textarea('details', null, array('placeholder' => 'Details','class' => 'form-control','style'=>'height:100px')) !!}
  32. {!! $errors->first('details', '<p class="alert alert-danger">:message</p>') !!}
  33. </div>
  34. </div>
  35. <div class="col-xs-12 col-sm-12 col-md-12">
  36. <div class="form-group">
  37. <strong>Captcha:</strong>
  38. {!! app('captcha')->display() !!}
  39. {!! $errors->first('g-recaptcha-response', '<p class="alert alert-danger">:message</p>') !!}
  40. </div>
  41. </div>
  42. <div class="col-xs-12 col-sm-12 col-md-12 text-center">
  43. <button type="submit" class="btn btn-primary">Submit</button>
  44. </div>
  45. {!! Form::close() !!}
  46. </div>
  47. </div>
  48. </div>
  49. </div>

Don't forget to add script file in head section.


  1. <script src="https://www.google.com/recaptcha/api.js"></script>

Now try this code in your application to validate your form using Google Nocapcha reCaptcha.

Phone: (+91) 8800417876
Noida, 201301
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
porno izle
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat
sohbet okey oyna
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort