Postal Code validation (Canadian)

I am using

function Validate()
var regEx = /[a-zA-Z][0-9][a-zA-Z](-| |)[0-9][a-zA-Z][0-9]/;
alert(‘Valid Postal Code’);
alert(‘Invalid Postal Code’);


to validate the postal code with an onBlur function for some reason even if i type in a valid postal code it comes as invalid. Can anyone clarify why?

You need to provide the value of the postalcode field to regEx.test()

i.e. regEx.test(document.getElementById(“postalcode”).value)

Assuming the postal code field has the id “postalcode”

I’m assuming btw the regex is correct, I don’t know how canadian postal codes are formatted.

thank you so much it works perfectly fine now