Monday 4 June 2018

Laravel Realtime validation using ajax



  Hi developers,

                                 This blog is about minor things on laravel
 We all know about email verification, it is like when users give their username, the data will be verified based on the existing or new one.

              This one is a must for current web applications. I'll share how it works using laravel,


In blade file
Textbox blur event
<input type="text" name="email" onblur="duplicateEmail(this)" class="form-control">
 
 
jQuery function
function duplicateEmail(element){ 
 var email = $(element).val(); 
 $.ajax({ 
type: "POST", 
 url: '{{url('checkemail')}}', 
 data: {email:email}, dataType: "json", 
success: function(res) { 
 if(res.exists){ 
 alert('true');  
}else{ 
 alert('false'); } 
 },
  error: function (jqXHR, exception) { 
 } 
 });
  }
route
Route::post('/checkemail',['uses'=>'PagesController@checkEmail']);

 
controller
public function checkEmail(Request $request){ 
$email = $request->input('email'); 
 $isExists = DB::table('User')->where('email',$email)->first();  
if($isExists){ 
 return response()->json(array("exists" => true)); 
 }else{
  return response()->json(array("exists" => false)); 
 }
  }
Thank you..for reading this one...
 
    

No comments:

Post a Comment