Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alfredo-wpmudev/98ddb903c3af720776575a5dbed5cf89 to your computer and use it in GitHub Desktop.
Save alfredo-wpmudev/98ddb903c3af720776575a5dbed5cf89 to your computer and use it in GitHub Desktop.
<?php
add_action('wp_footer','custom_js_form_validation');
function custom_js_form_validation(){
?>
<script>
jQuery( document ).ready(function($) {
jQuery("#forminator-module-14429 input[name='text-4']").keyup(function() {
let store_name = jQuery("#forminator-module-14429 input[name='text-4']").val();
let store_url = store_name.replace(/ /g, "-").toLowerCase();
jQuery("#forminator-module-14429 input[name='text-5']").val(store_url); console.log(store_url);
});
$(".store-name-forminator input").on('blur', function (e) {
var _field = $(this);
var _field_val = _field.val();
$.ajax({
url : '<?php echo admin_url( 'admin-ajax.php' ); ?>',
type: "POST",
data: {'action': 'check_store_availability_forminator', store: _field_val},
dataType: "json",
success: function(response) {
console.log(response.status);
if(response.status == 'unavailable'){
$('.custom-validation-store').remove();
$('.store-name-forminator input').parent().parent().append('<label class="forminator-label--validation custom-validation-store">Store is not available.</label>');
}else{
$('.custom-validation-store').remove();
$('.store-name-forminator input').parent().parent().append('<label class="forminator-label--validation custom-validation-store">Store is available.</label>');
}
}
});
});
});
</script>
<?php
}
add_action('wp_ajax_nopriv_check_store_availability_forminator', 'check_store_availability_forminator');
add_action('wp_ajax_check_store_availability_forminator', 'check_store_availability_forminator');
function check_store_availability_forminator() {
$response = array();
$store = sanitize_text_field( $_POST['store'] );
$stores = get_user_by('slug', $store);
if( $stores ){
$response['status'] = 'unavailable';
$response['text'] = __('Custom text that can be used later');
}else{
$response['status'] = 'available';
$response['text'] = __('Custom text that can be used later');
}
echo json_encode($response);
die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment