I'm trying to use a onblur function to validate a postcode field which is using a regex expression to test against, However it is returning false when it should be returning true. JS Fiddle: https://jsfiddle.net/gzr5csu4/2/
function validatePostcode(){
alert("onblur triggered!");
var postcodeRegEx = /^5[0-9]{3}$/;
var postcodeResult = postcodeRegEx.test(postcode);
if (postcodeResult == false){
alert("Postcode is not valid!");
}
return true;
}
I see no reason to why this isn't working, i'm fairly new to java-script so probably missing something very simple. The onblur function is triggered however when enetering "5251" into the postcode it returns false when it should not. Any help would be greatly appreciated.