How to check Email already exists in laravel with ajax?

·

,
Laravel 7.2 routing with route group auth guard check with prefix

Hello to all, welcome to therichpost.com. In this post, I will tell you, How to check Email already exists in laravel with ajax? I am doing with laravel first time in my website. Laravel is one of the top php mvc framework. I always share on my blog that what I like and what i faced in my past. This post is also related to my that. In this post we will validate email with ajax. Here is the working code to check Email already exists in laravel with ajax:

First Here is the html code and jquery script and you need to add this into your user.blade.php template:

<input class="form-control" id="cemail" type="email" name="email" placeholder="mail@example.com" onblur="checkmain(this.value)">

/*Js script*/

function checkmain(email)
{
$.ajax({
url: Laravel.appUrl+'/calendar/checkemail',
type: 'POST',
data: { email: email },
}).done(function(response) {
if(response == "Email Already In Use.")
{
alert("Email Already In Use.");
}

});
}

Here is code for laravel Route and you need to add this into you you can routes/web.php file:

Route::post('/checkemail', 'UserController@checkemail')-&gt;middleware('ajax');

And Finally here is the controller code and you need to add this into your UserController.php file:

public function checkemail(Request $req)
{

$PDO = \DB::connection()->getPdo();

$email = $req->email;
$emailcheck = $PDO->prepare("SELECT * FROM user WHERE email = ?");
$emailcheck->execute([$email]);
$emailcheck = $emailcheck->fetchAll(\PDO::FETCH_OBJ);
if(count($emailcheck)>0)
{
echo "Email Already In Use.";
}

//or
public function checkemail(Request $req)
{
$email = $req->email;
$emailcheck = DB::table('user')->where('email',$email)->count();

if($emailcheck > 0)
{
echo "Email Already In Use.";
}
}
}

 

If email will exists then you will alert with ajax There are so many code tricks in laravel and i will let you know all. Please do comment if you any query related to this post. Thank you. Therichpost.com

Comments

8 responses to “How to check Email already exists in laravel with ajax?”

  1. therichpost Avatar
    therichpost

    Thank you

  2. therichpost Avatar
    therichpost

    good and thank you

  3. therichpost Avatar
    therichpost

    public function checkemail(Request $req)
    {
    $email = $req->email;
    $emailcheck = DB::table(‘user’)->where(’email’,$email)->count();

    if($emailcheck > 0)
    {
    echo “Email Already In Use.”;
    }
    }

  4. Bratislav Avatar
    Bratislav

    Hi Ajay,

    Thank you very much for this post.

    Could you please explain in more details the following code line?
    url: Laravel.appUrl+’/calendar/checkemail’,

    Best regards.

  5. Ajay Malhotra Avatar

    Where I have written that?

  6. Bratislav Avatar
    Bratislav

    You wrote that in /*Js script*/

  7. Ajay Malhotra Avatar
    Ajay Malhotra

    that is for path reference.

  8. Bratislav Avatar
    Bratislav

    Thanks