Your regular expression also matches if there is a five-digit substring in your line. If you want to check "just five digits, nothing more", you need to anchor your regular expression:
var zipcode_regex = /^\d{5}$/; if (zipcode_regex.test($.trim($('#zipcode').val())) == false) alert('invalid zipcode');
And you can get it easier:
if (!(/^\s*\d{5}\s*$/.test($('#zipcode').val()))) { alert('invalid zipcode'); }
source share