jcadima
6/18/2019 - 8:27 AM

Laravel check if email exists via Ajax


https://stackoverflow.com/questions/46035204/realtime-validation-of-email-with-ajax-and-laravelif-email-is-already-in-databa

https://stackoverflow.com/questions/46466167/laravel-5-5-ajax-call-419-unknown-status

IN HEADER OF THIS FILE:
<meta name="csrf-token" content="{{ csrf_token() }}">



<div class="form-group">
    <div>
        <input type="email" name="email" id="email" class="form-control" placeholder="Email Address*" value="{{ old('email') }}">
    </div>
</div>  

<script>
function duplicateEmail(element) {
    $('input[type="email"]').on('blur', function(e) {
        console.log('EMAIL AJAX CALL') ;
        var email = $(element).val();
        $.ajax({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            type: "POST",
            url: '{{ route('checkEmail') }}',
            data: { email:email },
            dataType: "json",
            success: function(res) {
                if(res.exists){
                    console.log('Email Exists');
                }else{
                    console.log('Email Is Available');
                }
            },
            error: function (jqXHR, exception) {

            }
        });
    });
}

duplicateEmail('#email') ;