jQuery(function() {
var emailAddress = jQuery('#email_address');
emailAddress.on("change", function () {
var mail=emailAddress.val();
jQuery.ajax({
type: "POST",
url: "/customer/email/",
dataType: "json",
data: {email: mail},
success: function (exist) {
if (exist == 1) {
alert("exist");
} else if (exist == 0) {
alert("exist");
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert("Error " + jqXHR.status + " " + jqXHR.statusText);
}
});
});
});
Controller email.php
public function execute()
{
$customerEmail=$this->getRequest()->getParam('email');
$objectManager=\Magento\Framework\App\ObjectManager::getInstance();
$CustomerModel = $objectManager->create('Magento\Customer\Model\Customer');
$CustomerModel->setWebsiteId(1);
$CustomerModel->loadByEmail($customerEmail);
$userId = $CustomerModel->getId();
if ($userId) {
return 1;
} else {
return 0;
}
I want to check email before clicking create an account button using Ajax, i am not getting to do that, please help me out to solve this issue, thanks in advance.
It seems like you are trying to validate email address as customer enter his email address. For this you just need minor change in the email address field.
Create a controller and add you logic in execute method.
I hope this will be helpful to you.