vendredi 7 août 2015

UK postcode regex validation

I was looking for a valid UK postcode regex pattern, but seems that any of them work as a traditional or normal regex.

I test all patterns using this tool: https://regex101.com/

The regex I found in this document http://ift.tt/1PaoqPK is not working.

I tried all regex in this wiki page http://ift.tt/1DuT9Wo but nothing.

Am I doing something wrong?

I'm writing an Angular directive.

var regex = /[A-PR-UWYZa-pr-uwyz0-9][A-HK-Ya-hk-y0-9][AEHMNPRTVXYaehmnprtvxy0-9]?[ABEHMNPRVWXYabehmnprvwxy0-9]?{1,2}[0-9][ABD-HJLN-UW-Zabd-hjln-uw-z]{2}|(GIRgir){3} 0(Aa){2})$/g;

// add a parser that will process each time the value is 
// parsed into the model when the user updates it.
ngModel.$parsers.unshift(function(value) {
    // test and set the validity after update.
    var valid = regex.test(value);
    console.log(valid);
    ngModel.$setValidity('ukPostcode', valid);

    // if it's valid, return the value to the model, 
    // otherwise return undefined.
    return valid ? value : undefined;
});

Thank you.

Aucun commentaire:

Enregistrer un commentaire