Skip to content

Instantly share code, notes, and snippets.

@cartimize
Last active October 9, 2021 05:12
Show Gist options
  • Save cartimize/9fbaf44c0cf91b42160abde635235bda to your computer and use it in GitHub Desktop.
Save cartimize/9fbaf44c0cf91b42160abde635235bda to your computer and use it in GitHub Desktop.
Custom JS validation for the following fields (billing_address_1, shipping_address_1)
jQuery( document.body ).on( 'cartimize_shipping_address_1_validation', function(event, defaultResult, fieldValue){
//Your custom code. Return true or false else it will use default validation
//Example: Validation for specific countries.
var selectedCountry = jQuery("#shipping_country").val();
if(selectedCountry == 'NL' || selectedCountry == 'BEL' || selectedCountry == 'CZE'){
return /\d/.test(fieldValue);
}
});
jQuery( document.body ).on( 'cartimize_biiling_address_1_validation', function(event, defaultResult, fieldValue){
//Your custom code. Return true or false else it will use default validation
//Example: Validation for specific countries.
var selectedCountry = jQuery("#billing_country").val();
if(selectedCountry == 'NL' || selectedCountry == 'BEL' || selectedCountry == 'CZE'){
return /\d/.test(fieldValue);
}
});
// This will change the shipping and billing label based on country
jQuery( document.body ).on( 'country_to_state_changing', function(event){
var selectedCountry = jQuery("#shipping_country").val();
if(selectedCountry == 'NL' || selectedCountry == 'BEL' || selectedCountry == 'CZE'){
jQuery(".shipping_address_1 label").text('Street address (with house number)');
}else{
jQuery(".shipping_address_1 label").text('Street address');
}
selectedCountry = jQuery("#billing_country").val();
if(selectedCountry == 'NL' || selectedCountry == 'BEL' || selectedCountry == 'CZE'){
jQuery(".billing_address_1 label").text('Street address (with house number)');
}else{
jQuery(".billing_address_1 label").text('Street address');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment