Skip to content

Instantly share code, notes, and snippets.

@summersab
Created April 7, 2019 21:21
Show Gist options
  • Save summersab/bf7831a87d0a429602d8f3fa3324c411 to your computer and use it in GitHub Desktop.
Save summersab/bf7831a87d0a429602d8f3fa3324c411 to your computer and use it in GitHub Desktop.
FoxyCart Field Length Logic
/*
* Requires https://gist.github.com/summersab/ac6f801c8249d6940ca74d3f4634daf9
* Put this in your footer JS section to prevent customers from entering fields over 35 characters (UPS limit)
*/
$('#shipping_first_name, #shipping_last_name, #shipping_company, #shipping_address1, #shipping_address2').on('input',function(){
checkLengths();
});
$("body").on("focusout.fc", function() {
// I'm sure there's a better way to do this, but this makes sure to run the length validation logic AFTER the default FC validations. Otherwise, the custom error field highlighting briefly disappears.
var delay = setTimeout(function() {
checkLengths();
}, 5);
});
function checkLengths() {
var nameError = "First and last name must be less than 35 characters.";
var companyError = "Company last name must be less than 35 characters."
var addressError = "Address must be less than 35 characters";
if ($('#shipping_first_name').val().length + $('#shipping_last_name').val().length > 35) {
FC.util.addError('shipping_first_name', nameError);
FC.util.addError('shipping_last_name', nameError);
FC.checkout.updateErrorDisplay(document.querySelector('#shipping_first_name'),true);
FC.checkout.updateErrorDisplay(document.querySelector('#shipping_last_name'),true);
}
else {
FC.util.removeError('shipping_first_name', nameError);
FC.util.removeError('shipping_last_name', nameError);
if ($('#shipping_first_name').val().length != 0) {
FC.checkout.updateErrorDisplay(document.querySelector('#shipping_first_name'),false);
}
if ($('#shipping_last_name').val().length != 0) {
FC.checkout.updateErrorDisplay(document.querySelector('#shipping_last_name'),false);
}
}
if ($('#shipping_company').val().length > 35) {
FC.util.addError('shipping_company', companyError);
FC.checkout.updateErrorDisplay(document.querySelector('#shipping_company'),true);
}
else {
FC.util.removeError('shipping_company', companyError);
FC.checkout.updateErrorDisplay(document.querySelector('#shipping_company'),false);
}
if ($('#shipping_address1').val().length + $('#shipping_address2').val().length > 35) {
if ($('#shipping_address2').val().length == 0) {
FC.util.addError('shipping_address1', addressError);
FC.checkout.updateErrorDisplay(document.querySelector('#shipping_address1'),true);
}
else if ($('#shipping_address1').val().length == 0) {
FC.util.addError('shipping_address2', addressError);
FC.checkout.updateErrorDisplay(document.querySelector('#shipping_address2'),true);
}
else {
FC.util.addError('shipping_address1', addressError);
FC.util.addError('shipping_address2', addressError);
FC.checkout.updateErrorDisplay(document.querySelector('#shipping_address1'),true);
FC.checkout.updateErrorDisplay(document.querySelector('#shipping_address2'),true);
}
}
else {
FC.util.removeError('shipping_address1', addressError);
FC.util.removeError('shipping_address2', addressError);
if ($('#shipping_address1').val().length != 0) {
FC.checkout.updateErrorDisplay(document.querySelector('#shipping_address1'),false);
}
FC.checkout.updateErrorDisplay(document.querySelector('#shipping_address2'),false);
}
FC.util.notifyErrors();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment