Limit Characters on Shopify Checkout to English Characters Only
Where was this used:
This issue was first on the Babymetal store.
Reason for adding:
Use this code when customers add non-english characters to the checkout. For example the Baby Metal store received a lot of customers from Japan writing Japanese characters.
Ask shopify to 'enable checkout editing' (This is required and cannot be done ourselves).
Open checkout.liquid and add the following code to the bottom of the file.
<script>
$(document).ready(function() {
$('input').bind('keyup blur', function() {
//alert('In here with' + $(this).val())
//var regex = new RegExp("/^[a-z ]+$/i");
if($(this).val().match(/[^\u0000-\u007F\u0080-\u00FF\u0100-\u017F\u0180-\u024F\u1E00-\u1EFF]/g)){
alert('Invalid Character usage. Please Use English characters only.');
$(this).val($(this).val().replace(/[^\u0000-\u007F\u0080-\u00FF\u0100-\u017F\u0180-\u024F\u1E00-\u1EFF]/g, ''))
return false;
}
});
});
</script>
Test
Copy and paste the following Chinese characters in any of the input fields. You should receive an alert saying to use English characters only.
最高
Helpful Resources & additional reading
Read about regular expressions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Read about match function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match
Unicode blocks: http://jrgraphix.net/research/unicode_blocks.php
Regular Expression generator: http://kourge.net/projects/regexp-unicode-block