Created
April 26, 2024 07:51
-
-
Save pramodjodhani/3e592b7583428b30bf2d8ce70fec0c9d to your computer and use it in GitHub Desktop.
flux - custom field validation on JS side
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Flux checkout birthday field custom validation. | |
*/ | |
add_action( 'wp_footer', function() { | |
?> | |
<script> | |
jQuery( function($) { | |
$( 'form.checkout' ).on( 'blur', '.birthday-field input', function() { | |
// todo change your validation logic here. | |
var age = parseInt($( this ).val()); | |
// If there is an error then add error class and show error message. | |
if ( age < 18 ) { | |
$p = $( this ).closest('.form-row'); | |
$p.addClass( 'woocommerce-invalid' ); | |
var error_msg = 'You must be at least 18 years old to place an order'; | |
if ( $p.find( '.error' ).length ){ | |
$p.find( '.error' ).html( error_msg ); | |
} else { | |
$p.append( `<span class="error">${error_msg}.</span>` ); | |
} | |
} else { | |
// If there is no error then remove error class and hide error message. | |
$p.removeClass( 'woocommerce-invalid' ); | |
$p.find( '.error' ).remove(); | |
} | |
} ); | |
} ); | |
</script> | |
<?php | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment