Regular Expression to validate UK National Insurance Number

后端 未结 8 1217
谎友^
谎友^ 2020-12-24 03:05

I have the following regular expression which validates the British National Insurance Number

^([a-zA-Z]){2}( )?([0-9]){2}( )?([0-9]){2}( )?([0-9]){2}( )?([         


        
相关标签:
8条回答
  • 2020-12-24 03:29
    • Just found this when I was digging around to find a proper regex. So just thought of sharing this DWP NINO format-validation.
    • Under the hood, they are using the following regex "(^(?!BG)(?!GB)(?!NK)(?!KN)(?!TN)(?!NT)(?!ZZ)[A-Z&&[^DFIQUV]][A-Z&&[^DFIOQUV]][0-9]{6}[A-D ]$)"
    • I found this very useful and works as expected older version of maven also supports NINO with ending character as space.
    • List item

    Nino Validator meets all the following criteria.

    • Must be 9 characters.
    • First 2 characters must be alpha.
    • Next 6 characters must be numeric.
    • Final character can be A, B, C, D or space.
    • First character must not be D,F,I,Q,U or V.
    • Second characters must not be D, F, I, O, Q, U or V.
    • First 2 characters must not be combinations of GB, NK, TN or ZZ (the term combinations covers both GB and BG etc.)
    0 讨论(0)
  • 2020-12-24 03:32

    Actually, NIN doesn't allow D, F, I, Q, U or V for the first two letters and doesn't allow O for the second letter; on top of this, the prefix letters can not be BG, GB, NK, KN, TN, NT and ZZ. Also the suffix letter is either A, B, C or D, but may be represented by a space if unknown. - http://en.wikipedia.org/wiki/National_Insurance_number#Format

    As such, a more valid check would be (I have only supplied a version with capital letters, can easily be altered for lower-case):

    ^(?!BG)(?!GB)(?!NK)(?!KN)(?!TN)(?!NT)(?!ZZ)(?:[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z])(?:\s*\d\s*){6}([A-D]|\s)$
    
    0 讨论(0)
提交回复
热议问题