Created
August 24, 2020 14:00
-
-
Save michelarteta/98af40c96e24fd8de6bfb21708c23e39 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
$(document).ready(function() { | |
var poboxRegex = new RegExp('(?:P(?:ost(?:al)?)?[\\.\\-\\s]*(?:(?:O(?:ffice)?[\\.\\-\\s]*)?B(?:ox|in|\\b|\\d)|o(?:ffice|\\b)(?:[-\\s]*\\d)|code)|box[-\\s\\b]*\\d)', 'i'); | |
var phoneRegex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/; | |
var lettersAndNumbersRegex = /^(?=.*[0-9]{2,})(?=.*[a-z]{3,})([\sa-zA-Z0-9\\'\\-]+)$/; | |
var repeatCharactersRegex = /([a-zA-Z]{2,}|[0-9]{3,})\1$/; | |
// ^(?!.*?(.+)\1)([\w@+$!.-]+){8,20}$ | |
function showError(errorContainer, element, msg){ | |
$(errorContainer).remove(); | |
$(element).closest('.field').find(".error-message").remove(); | |
$(element).closest('.field').addClass("field--error"); | |
$(element).parent().append('<div class="error-message" id="' + errorContainer.substring(1) + '" style="margin-top: 6px; color: red;">' + msg + '</div>'); | |
} | |
function validateAddress(address){ | |
if (poboxRegex.test(address)) { | |
showError('#error-address1', '#checkout_shipping_address_address1', 'Sorry we do not ship to P.O. Boxes'); | |
return false; | |
} | |
if (!lettersAndNumbersRegex.test(address)) { | |
showError('#error-address1', '#checkout_shipping_address_address1', 'Please enter a valid address.'); | |
return false; | |
} | |
if (repeatCharactersRegex.test(address)) { | |
showError('#error-address1', '#checkout_shipping_address_address1', 'The address is invalid, please fix it.'); | |
return false; | |
} | |
if(address.length > 32){ | |
showError('#error-address1', '#checkout_shipping_address_address1', 'We are sorry for the inconvenience but our system only accepts one address line with a max of 32 characters. Please abbreviate when possible.'); | |
return false; | |
} | |
return true; | |
} | |
function validatePhone(phone){ | |
if (!phoneRegex.test(phone)) { | |
showError('#error-phone', '#checkout_shipping_address_phone', 'Enter a valid phone number please.'); | |
return false; | |
} | |
return true; | |
} | |
$(document).on('click', "[data-step=contact_information] #continue_button", function(e) { | |
$(this).attr('type', 'button'); | |
var address = $.trim($('#checkout_shipping_address_address1').val()); | |
var phone = $.trim($('#checkout_shipping_address_phone').val()); | |
var canSubmit = false; | |
if (validateAddress(address) && validatePhone(phone)) { | |
canSubmit = true; | |
} | |
if(canSubmit){ | |
$(this).attr('type', 'submit'); | |
$(this).trigger('click'); | |
} | |
}); | |
$("#checkout_shipping_address_address1").on("change keyup", function() { | |
var address = $(this).val(); | |
var canSubmit = false; | |
canSubmit = validateAddress(address); | |
if(canSubmit){ | |
$('#error-address1').remove(); | |
} | |
}); | |
$('#checkout_shipping_address_phone').on("change keyup", function() { | |
var phone = $(this).val(); | |
var canSubmit = false; | |
canSubmit = validatePhone(phone); | |
if(canSubmit){ | |
$('#error-phone').remove(); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment