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

2 Answers 2

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}/

Sign up to request clarification or add additional context in comments.

Comments

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}))$

3 Comments

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
The regexp does match spaces. See the 5th char.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.