0

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

Shahzeb Khan
  • 3,582
  • 8
  • 45
  • 79

2 Answers2

0

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: O.P. 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}))$
Community
  • 1
  • 1
MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • Thanks for response : i have removed spaces from the post codes but its still saying that post code is not valide. For example i add "W1A1HQ" but its still invalid – Shahzeb Khan Apr 22 '14 at 08:02
  • The regexp *does* match spaces. See the 5th char. – tobib Apr 22 '14 at 08:23
  • I said: "(if not in the first block and the last one...)". And obviously the test case does not match the first (or-ed) block "(GIR 0AA)", and it contains many more blanks than the single one matchable by the last block. – MarcoS Apr 22 '14 at 10:26
0

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 [A-Z-[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|[A-Z][0-9][0-9]?|[A-Z][A-Z][0-9][0-9]?|[A-Z][0-9][A-HJKSTUW]|[A-Z][A-Z][0-9][ABEHMNPRVWXY]) [0-9][A-Z]{2}/

tobib
  • 2,114
  • 4
  • 21
  • 35