var elem = document.getElementById("Cpostalcode"); var pattern = elem.getAttribute("pattern"); var re = new RegExp(pattern); if (re.test(elem.value)) { // Pattern matches! } else { // Pattern does NOT match. }