I'm trying to check inserted value for billing_email field in WooCommerce checkout to know if it is existed or not.
Here is code in functions.php
add_action('wp_enqueue_scripts', 'live_validation' );
add_action('wp_ajax_validate_email', 'validate_email_input');
add_action('wp_ajax_nopriv_validate_email', 'validate_email_input');
function live_validation() {
wp_enqueue_script( "validate_email", get_stylesheet_directory_uri() . '/check-email.js', array( 'jquery' ) );
wp_localize_script( "validate_email", "validateEmail", array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
function validate_email_input() {
global $wpdb;
$email = $_POST['billing_email'];
if ( email_exists($email) ) {
echo 'existed';
} else {
echo 'not exist';
}
exit;
}
And this is js file
jQuery(document).ready(function($) {
$('input[name=billing_email]').change(function() {
var input_value = $(this).val();
$.post( validateEmail.ajaxurl, { action:'validate_email', billing_email:input_value }, function(data) {
$('.message').html(data);
});
});
});
HTML form
<p class="form-row form-row form-row form-row-first validate-required woocommerce-validated" id="billing_email_field">
<label for="billing_email" class="">Email <abbr class="required" title="required">*</abbr></label>
<input type="text" class="input-text " name="billing_email" id="billing_email" placeholder="Email" value="abc@gmail.com"></p>
<span class="message"></span>
I can see admin-ajax file fired when input value of billing_email changed, but the notification didn't show.
Please help. Thanks.