In this tutorial you will learn about the Laravel 5.8 jQuery Form Validation and its application with practical example.
Laravel 5.8 jQuery Form Validation
Input validation is a process where we check whether the input data provided by user or client is in correct format or not. If the input data fails to satisfy the validation rules then the user is responded with an error response code and the user is requested to resubmit the form with correct information. Generally Form validation is performed at server side, but can be performed at both the server and client side.
In this simple example, I’ll show you how to implement client side validation in laravel using Jquery Form Validation plugin. In this tutorial, you will learn to validate the form data in the client side before it is submitted to server. When the form data satisfies the validation it will submitted to server for further processing.
Create Laravel View Files
In this step, we will create laravel view/blade file to perform display form. Lets create a blade file “index.blade.php” in “resources/views/jqueryFormValidation/” directory and put the following code in it respectively.
resources/views/jqueryFormValidation/contact_form.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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Laravel 5.8 JQuery Form Validation Example - W3Adda.com</title> <link rel="stylesheet" href="{{asset('css/app.css')}}"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"></script> <script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script> <script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script> <style type="text/css"> .error{ color: red; } </style> </head> <body> <div class="container"> <h2>Laravel 5.8 JQuery Form Validation Example - W3Adda.com</h2><br/> <form method="post" action="{{url('validation')}}" id="form"> @csrf <div class="row"> <div class="col-md-4"></div> <div class="form-group col-md-4"> <label for="Name">Name:</label> <input type="text" class="form-control" name="name"> </div> </div> <div class="row"> <div class="col-md-4"></div> <div class="form-group col-md-4"> <label for="Email">Email:</label> <input type="text" class="form-control" name="email"> </div> </div> <div class="row"> <div class="col-md-4"></div> <div class="form-group col-md-4"> <label for="Number">Phone:</label> <input type="text" class="form-control" name="number"> </div> </div> <div class="row"> <div class="col-md-4"></div> <div class="form-group col-md-4"> <label for="Min Length">Min Length(minium 5):</label> <input type="text" class="form-control" name="minlength"> </div> </div> <div class="row"> <div class="col-md-4"></div> <div class="form-group col-md-4"> <label for="Max Length">Max Length(maximum 8):</label> <input type="text" class="form-control" name="maxlength"> </div> </div> <div class="row"> <div class="col-md-4"></div> <div class="form-group col-md-4"> <label for="Min Value">Min Value(minium 1):</label> <input type="text" class="form-control" name="minvalue"> </div> </div> <div class="row"> <div class="col-md-4"></div> <div class="form-group col-md-4"> <label for="Max Value">Max Value(maximum value 100):</label> <input type="text" class="form-control" name="maxvalue"> </div> </div> <div class="row"> <div class="col-md-4"></div> <div class="form-group col-md-4"> <label for="Range">Range(minium 20, maximum 40):</label> <input type="text" class="form-control" name="range"> </div> </div> <div class="row"> <div class="col-md-4"></div> <div class="form-group col-md-4"> <label for="Range">URL:</label> <input type="text" class="form-control" name="url"> </div> </div> <div class="row"> <div class="col-md-4"></div> <div class="form-group col-md-4"> <input type="file" name="filename"> </div> </div> <div class="row"> <div class="col-md-4"></div> <div class="form-group col-md-4" style="margin-top:60px"> <button type="submit" class="btn btn-success">Submit</button> </div> </div> </form> </div> <script> $(document).ready(function () { $('#form').validate({ // initialize the plugin rules: { name: { required: true }, email: { required: true, email: true }, number: { required: true, digits: true }, minlength: { required: true, minlength: 5 }, maxlength: { required: true, maxlength: 8 }, minvalue: { required: true, min: 1 }, maxvalue: { required: true, max: 100 }, range: { required: true, range: [20, 40] }, url: { required: true, url: true }, filename: { required: true, extension: "jpeg|png" }, } }); }); </script> </body> </html> |
Load JS and CSS file
For that first, we need to load jQuery Form validation plugin along with jQuery to validate input fields and also use bootstrap css file.
1 2 3 4 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"></script> <script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script> |
Create Laravel Controller
Next, we have to create a controller to display contact form and to handle form validation and submit operations. Lets Create a controller named ContactController using command given below –
1 |
php artisan make:controller jqueryFormValidation/ContactController |
Once the above command executed, it will create a controller file ContactController.php in app/Http/Controllers/jqueryFormValidation directory.
Open the jqueryFormValidation/ContactController.php file and put the following code in it.
app/Http/Controllers/jqueryFormValidation/ContactController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php namespace App\Http\Controllers\jqueryFormValidation; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class ContactController extends Controller { public function index() { return view('jqueryFormValidation.index'); } public function store(Request $request) { // } } |
Define Laravel Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 |
Route::get('jquery-form-validation', 'jqueryFormValidation\ContactController@index'); |
Start Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
http://localhost:8000/jquery-form-validation
Output 1:-
Output 2:-
Validate form data and display error messages.