How to validate a domain name using laravel and ajax.if we enter a domain name(Eg:http://test.com)
through ajax call we will validate the domain
Below are the routes,controller,model,view files
routes.php- in the latest version laravel web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php /* |-------------------------------------------------------------------------- | 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('/', function () { return view('welcome'); }); Route::get('urlvalidator', 'WelcomeController@urlvalidator'); Route::post('checkDomain', 'WelcomeController@checkDomain'); Route::post('urlSubmitPage', 'WelcomeController@urlSubmitPage'); |
controller-Welcomeontroller.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
<?php namespace App\Http\Controllers; use App\Models\Country; use App\Models\State; use Illuminate\Http\Request; class WelcomeController extends Controller { /* |-------------------------------------------------------------------------- | Welcome Controller |-------------------------------------------------------------------------- | | This controller renders the "marketing page" for the application and | is configured to only allow guests. Like most of the other sample | controllers, you are free to modify or remove it as you desire. | */ /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest'); } /** * Show the application welcome screen to the user. * * @return Response */ public function index() { return view('welcome'); } public function contactf() { return view('pages.contact'); } public function urlvalidator() { return view('urlvalidation.urlvalidation'); } public function checkDomain(Request $request) { $url = $request->input('url'); $file_headers = @get_headers($url); $file_header_result = explode(" ", $file_headers[0]); $valid_response_code = array("400", "401", "402", "403", "404", "405", "406", "407", "408", "409", "410", "411", "412", "413", "414", "415", "416", "417", "500", "501", "502", "503", "504", "505"); if (!$file_headers || in_array($file_header_result[1], $valid_response_code)) { $exists = "false"; } else { $exists = "true"; } echo $exists; } public function urlSubmitPage() { echo "Submitted"; } } |
views file-urlvalidation/urlvalidation.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
<html> <head> <title>Validating a Domain using Laravel</title> <meta name="csrf-token" content="{{ csrf_token() }}" /> <link href='//fonts.googleapis.com/css?family=Lato:100' rel='stylesheet' type='text/css'> <style> body { margin: 0; padding: 0; width: 100%; height: 100%; color: #B0BEC5; display: table; font-weight: 100; font-family: 'Lato'; } .container { text-align: center; display: table-cell; vertical-align: middle; } .content { text-align: center; display: inline-block; } .title { font-size: 96px; margin-bottom: 40px; } .quote { font-size: 24px; } </style> </head> <body> <div class="container"> <form method="post" action="{{ URL::asset('urlSubmitPage') }}" name ="url_submit_form" id='url_submit_form' class='form-horizontal' enctype="multipart/form-data"> <input type="hidden" name="_token" value="{{ csrf_token() }}"> <div class="content"> <div class="title">Domain name validation in laravel</div> <div class="form-group"> <input type="text" id="domain_url" name="domain_url"> </div> <span id="validate_error" style="color:red;"></span> <span id="validate_result" style="color:red;font-size:14px;"></span> <button name="check_url" id="check_url">Check Urls</button> </div> </form> </div> </body> </html> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script> $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); $('#url_submit_form').submit(function() { var siteUrl ="{{url('checkDomain')}}"; var entered_url=$('#domain_url').val(); var dataString = 'url='+entered_url; if(entered_url=="") { $('#validate_error').html('Please enter a URL'); return false; } else { $.ajax({ url:siteUrl, async:true, type:"POST", data:dataString, dataType:"html", beforeSend:function(){ $('#validate_error').html('Please Wait Loading...'); }, success:function(data) { alert(data); if(data=='false') { $('#validate_result').html('Invalid URL'); return false; } else { $('#validate_result').html('False'); } } }); } return false; }); </script> |