Magento check if customer already exists by email with jQuery

Magento check if customer already exists by email with jQuery

July 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento check if customer already exists by email with jQuery

Magento: Checking for Existing Customers by Email Using jQuery

Hello developers! Today we’re going to address a common issue faced during checkout—offline customers trying to register with an already-existing email address. We’ll create a simple yet effective solution using PHP and jQuery to prevent frustration for both you and your clients.

The Current Approach: PHP Script in Controller

Our developer Shane has already created a great PHP script that checks if a given email exists in the customer database:

public function mailExists(Varien_Object $customer, $mail, $websiteId)
{
    // ... (rest of the code)
}

public function formAction()
{
    // ... (rest of the code)

    if($this->mailExists($customer, $email, $websiteId))
    {
        echo 'mail <u>'.$email.'</u> exists containing customer id '.$this->mailExists($customer, $email, $websiteId);
    }
    else{
        $this->_formPost();
    }
}

This works perfectly, but it resets all entered data on the checkout page when someone presses submit. Not ideal for user experience!

The Goal: Real-time Checking with jQuery

Shane wants to notify users instantly if they enter an existing email address using a jQuery script that runs as soon as text is entered into the email field.

public function testAction()
{
    echo("
        <script src='http://code.jquery.com/jquery-latest.js'></script>
        <script type='text/javascript'>
            $(document).ready(function()
            {
                $('#email').onChange(function()
                {
                    alert('input');
                });
            });
        </script>
    ");
    $this->_testPost();
}

This script prints “input” in an alert box when the email field is changed, but it doesn’t actually check for existing emails. That’s where we come in!

Implementing the PHP Check with jQuery

To make this work, we’ll need to:

  1. Send an AJAX request from jQuery to a new PHP action.
  2. In that PHP action, call our mailExists function and return the result (exists or not).
  3. Use the returned value in jQuery to show/hide error messages or styles.

Here’s how you can implement this:

public function ajaxCheckAction()
{
    $customer = Mage::getModel('customer/customer');
    $websiteId = Mage::app()->getWebsite()->getId();
    $email = $_POST['email'];

    if($this->mailExists($customer, $email, $websiteId))
        echo json_encode(['exists' => true]);
    else
        echo json_encode(['exists' => false]);

    exit;
}

Then in your jQuery:

$(document).ready(function()
{
    $('#email').on('input', function()
    {
        var email = $(this).val();
        if (email.length > 0) {
            $.ajax({
                url: 'url_to_your_ajax_check_action',
                type: 'POST',
                dataType: 'json',
                data: {email: email},
                success: function(response)
                {
                    if (response.exists === true)
                        alert('Email already exists!');
                    else
                        alert('Email is available!');
                },
            });
        }
    });
});

Replace 'url_to_your_ajax_check_action' with the actual URL to your ajaxCheckAction.

Conclusion

By following these steps, you’ll have a seamless solution that instantly notifies customers whether their entered email address already exists in the system. This significantly improves user experience and saves everyone time.

Remember, the key is to use AJAX to send real-time data from jQuery to PHP, then process it there and return the result. It’s a classic server-client communication pattern!

Don’t forget to check out Laravel Company’s blog for more tips and tricks on Magento development!

Checkout Database API Email

Our network