How To Check Email Already Exist Or Not In Laravel

Websolutionstuff | Aug-07-2020 | Categories : Laravel PHP jQuery

In this article, we will show how to check whether the email already exists or not in laravel. Also, we will check laravel unique email validation. And if the email id exists in the database or not using the ajax call jquery.

Many times users register with duplicate or existing email IDs and it is very difficult to maintain data or records in the database. So we will check email exists or not, if the email already registers with the same email id then we will display an error message to the user like the email already exists.

So, let's see laravel check whether the email address already exists or not in the database.

Here I have created a controller and added the below code in my controller.

Use Response;
public function userEmailCheck(Request $request)
{

	$data = $request->all(); // This will get all the request data.
	$userCount = User::where('email', $data['email']);
	if ($userCount->count()) {
		return Response::json(array('msg' => 'true'));
	} else {
		return Response::json(array('msg' => 'false'));
	}
}

 

 

Now create a blade file for view and add javascript validation in this file.

<div class="form-group row">
	<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

	<div class="col-md-6">
		<input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">

		@error('email')
			<span class="invalid-feedback" role="alert">
				<strong>{{ $message }}</strong>
			</span>
		@enderror
	</div>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>  // if required
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.2/jquery.validate.min.js" type="text/javascript"></script>

<script>    
    var email =  $("#email").val();
    $('#registration').validate({
        rules: {            
            email: {
                required: true,
                email: true,
                remote: {
                    url: '{{url('user/checkemail')}}',
                    type: "post",
                    data: {
                        email:$(email).val(),
                        _token:"{{ csrf_token() }}"
                        },
                    dataFilter: function (data) {
                        var json = JSON.parse(data);
                        if (json.msg == "true") {
                            return "\"" + "Email address already in use" + "\"";
                        } else {
                            return 'true';
                        }
                    }
                }
            }
        },
        messages: {            
            email: {
                required: "Email is required!",
                email: "Enter A Valid EMail!",
                remote: "Email address already in use!"
            }
        }
    });
</script>

 


You might also like:

Recommended Post
Featured Post
Laravel 8 Yajra Datatable Example Tutorial
Laravel 8 Yajra Datatable Exam...

In this article, we will see the laravel 8 yajra datatable example tutorial. Datatable provides users to many...

Read More

Sep-30-2020

How To File Upload In React JS
How To File Upload In React JS

In this article, we will see how file uploads in react js. File uploading means a user from a client machine wants...

Read More

Sep-05-2022

How To Encrypt And Decrypt String In Laravel 8
How To Encrypt And Decrypt Str...

In this example we will see how to encrypt and decrypt string in laravel 8 using crypt helper, As we all know laravel fr...

Read More

May-07-2021

How To Validate Phone Number In Laravel 10
How To Validate Phone Number I...

In this article, we will see how to validate phone numbers in laravel 10. Here, we will learn about mobile number v...

Read More

May-10-2023