I am using form validation plugin and using regular expression to validate UK post code. I am using This reference regular expression in which author says that it can validate all post code. But when i try to submit the form , for every kind of string it says invalid. I have tried the following (samples taken from wikipedia Here is page
W1A 1HQ
EC1A 1BB
M1 1AA
B33 8TH
Here is my JS code
$(document).ready(function(){
$.validator.addMethod("regex", function(value, element, regexp) {
var re = new RegExp(regexp);
console.debug(re.test(value))
return this.optional(element) || re.test(value);
},
"Post code is not valid"
);
$("#addPropertyForm").validate({
rules : {
postCode : {
required : true,
regex: "(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})"
}
},
messages : {
postCode : {
required : 'Post code is required',
}
},
onkeyup: false,
onblur: true,
focusCleanup: true,
focusInvalid: false
});
});
Can any one kindly help me what is wrong with this code
Without deeply inspecting the regex code, I can see it does not match any space (if not in the first block and the last one...): is it possible that you just have to strip spaces from your postcode (" W1A 1HQ EC1A 1BB M1 1AA B33 8TH
" in your test case, if I understand correctly) before matching against the regex?
UPDATE : OP did correct the question (the test codes where 4 distinct ones...), so my above answer doesn't make any sense anymore... :-)
Please be warned regexps come in different "flavors"... For example, JS flavor differs from Perl's one... Probably you can't use the given regexp in JavaScript 'as-is', but you probably should "cook' it in JS flavor... :-)
See also here and here , where a (probably) better regexp is given:
^(([gG][iI][rR] {0,}0[aA]{2})|((([a-pr-uwyzA-PR-UWYZ][a-hk-yA-HK-Y]?[0-9][0-9]?)|(([a-pr-uwyzA-PR-UWYZ][0-9][a-hjkstuwA-HJKSTUW])|([a-pr-uwyzA-PR-UWYZ][a-hk-yA-HK-Y][0-9][abehmnprv-yABEHMNPRV-Y]))) {0,}[0-9][abd-hjlnp-uw-zABD-HJLNP-UW-Z]{2}))$
There are some strange bits about this.
First of all, the example postcode you posted is in fact the concatenation of four postcodes: W1A 1HQ, EC1A 1BB, M1 1AA and B33 8TH.
Second, the regexp is written in a different dialext than the one used in JavaScript (I do not know which one). The expression [AZ-[IJZ]]
whould probably mean "any capital letter other than I
, J
and Z
". In Javascript it means "Any capital letter or -
or [
followed by a ]
". There are also some unecessary parantheses in it.
I did not take the time to fix the regexp. However I removed some bits to check if there were any other issues. So this one should match all valid postcodes (and also many invalid ones):
/(GIR 0AA|[AZ][0-9][0-9]?|[AZ][AZ][0-9][0-9]?|[AZ][0-9][A-HJKSTUW]|[AZ][AZ][0-9][ABEHMNPRVWXY]) [0-9][AZ]{2}/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.