I am trying to validate postal-code input with so-called regular expressions. Below is my code which doesn't seem to work for some reason. I need a solution completely in pure JS, not in other frameworks.
function postalCodeValidate() {
var postalCode = document.getElementbyID("postalcode").value;
var errorMessage = document.getElementbyID("pcodeerror").innerHTML;
var postalPattern = /^\d{2}-\d{3}$/;
if (POSTALCODE == "") {
errorMessage = "You must enter a postal code!";
} else if (POSTALCODE.match(postalPattern) == null) {
errorMessage = "Wrong postal code format (00-000)";
}
}
<div class="frm">
<form>
<h5>Enter adress details</h5>
<div class="form-group">
<label for="postalcode">Postal Code</label>
<input type="text" class="form-control" id="postalcode" placeholder="Postal Code (00-000)">
<div id="pcodeerror"></div>
</div>
<button type="button" onclick="postalCodeValidate();" class="btn btn-primary">Submit</button>
<a href="#Register">
<h6>Register</h6>
</a>
</form>
</div>
errorMessage = "..."won't change the content of the element with idpcodeerror