admin管理员组

文章数量:1400559

Is it possible to validate a UK driving license with the following rules?

  • Digit 1–5: Letters A-Z or 9's
  • Digit 6-11: Numbers 0-9
  • Digit 12–13: Letters A-Z or 9's
  • Digit 14: Number 0-9
  • Digit 15–16: Two letters A-Z

So far I have the following but it is not working:

var regex = /^[A-Z0-9]{5}\d[0156]\d([0][1-9]|[12]\d|3[01])\d[A-Z0-9]{3}[A-Z]{2}$/

The following should pass:

  [
    'FARME100165AB5EW',
    'MAR99740614BC2TL',
    'MARTI740614A92TL',
    'MARTI740614992TL',
    '99999740614992TL'
  ].forEach(drivingLicenceNumber => regex.test(drivingLicenceNumber)

Only the first element of the array passes.

And the following should fail

  [
    '1FARM382940AZ9AZ',
    'F2ARM382940AZ9AZ',
    'FA3RM382940AZ9AZ',
    'FAR4M382940AZ9AZ',
    'FARM5382940AZ9AZ'
  ].forEach(drivingLicenceNumber => !regx.text(drivingLicenceNumber)

Is it possible to validate a UK driving license with the following rules?

  • Digit 1–5: Letters A-Z or 9's
  • Digit 6-11: Numbers 0-9
  • Digit 12–13: Letters A-Z or 9's
  • Digit 14: Number 0-9
  • Digit 15–16: Two letters A-Z

So far I have the following but it is not working:

var regex = /^[A-Z0-9]{5}\d[0156]\d([0][1-9]|[12]\d|3[01])\d[A-Z0-9]{3}[A-Z]{2}$/

The following should pass:

  [
    'FARME100165AB5EW',
    'MAR99740614BC2TL',
    'MARTI740614A92TL',
    'MARTI740614992TL',
    '99999740614992TL'
  ].forEach(drivingLicenceNumber => regex.test(drivingLicenceNumber)

Only the first element of the array passes.

And the following should fail

  [
    '1FARM382940AZ9AZ',
    'F2ARM382940AZ9AZ',
    'FA3RM382940AZ9AZ',
    'FAR4M382940AZ9AZ',
    'FARM5382940AZ9AZ'
  ].forEach(drivingLicenceNumber => !regx.text(drivingLicenceNumber)
Share Improve this question edited Aug 3, 2017 at 20:11 Chris Forrence 10.1k11 gold badges49 silver badges64 bronze badges asked Aug 3, 2017 at 20:01 dagda1dagda1 29k67 gold badges255 silver badges477 bronze badges 5
  • Are there errors? Post the relevant code block that you are using because a quick test in regex101 suggests that the regex might be working. (There are missing parentheses in the fragments you've posted.) – Andy G Commented Aug 3, 2017 at 20:06
  • Which ones are not doing what they should? I tried 'FARME100165AB5EW' and it passed – Ruan Mendes Commented Aug 3, 2017 at 20:07
  • /^[A-Z0-9]{5}\d[0156]\d([0][1-9]|[12]\d|3[01])\d[A-Z0-9]{3}[A-Z]{2}$/.test('MAR99740614BC2TL') – dagda1 Commented Aug 3, 2017 at 20:08
  • 1 Only the first one passes – dagda1 Commented Aug 3, 2017 at 20:09
  • Your regex doesn't make a lot of sense. [A-Z0-9]{5} means look for five alphanumeric characters, that's good. However, your next piece seems like it should be \d{6} and I'm not sure what you are trying to do with \d[0156]\d([0][1-9]|[12]\d|3[01])\d – Ruan Mendes Commented Aug 3, 2017 at 20:24
Add a ment  | 

2 Answers 2

Reset to default 5

This should work:

^[A-Z9]{5}\d{6}[A-Z9]{2}\d[A-Z]{2}$

Explanation:

  • [A-Z9]{5} Digit 1–5: Letters A-Z or 9's
  • \d{6} Digit 6-11: Numbers 0-9
  • [A-Z9]{2} Digit 12–13: Letters A-Z or 9's
  • \d Digit 14: Number 0-9
  • [A-Z]{2} Digit 15–16: Two letters A-Z

var regex = /^^[A-Z9]{5}\d{6}[A-Z9]{2}\d[A-Z]{2}$$/

console.log('all pass');
  [
    'FARME100165AB5EW',
    'MAR99740614BC2TL',
    'MARTI740614A92TL',
    'MARTI740614992TL',
    '99999740614992TL'
  ].forEach(drivingLicenceNumber => {
    console.log(drivingLicenceNumber, regex.test(drivingLicenceNumber));
  });  
console.log('all fail');
  [
    '1FARM382940AZ9AZ',
    'F2ARM382940AZ9AZ',
    'FA3RM382940AZ9AZ',
    'FAR4M382940AZ9AZ',
    'FARM5382940AZ9AZ'
  ].forEach(drivingLicenceNumber => {
    console.log(drivingLicenceNumber, regex.test(drivingLicenceNumber));
  }); 

The regex provided is for valid UK driving licenses and the rules you've set do not match that.

Format is:

5 letters (first 5 of surname or 9s => 9s make up the rest if surname is not 5 digits long)

[A-Z9]{5}

1 number (first from decade of birth (eg. 1957 = 5))

\d

2 numbers (month of birth taking into account females have an extra 50 added onto this so 09 would be 69 and 12 would be 72) Therefore the first digit of this can only be 0,1,5 or 6 and the second can be anything between 0 and 9 (you can go deeper here to not get values like 19 etc but I'm not gunna bother)

[0156]

\d

2 numbers (day of the month you are born on)

([0][1-9]|[12]\d|3[01])

1 number (last from decade of birth (eg. 1957 = 7))

\d

2 letters/numbers => Your initials (eg - Paul Simon = PS) 9s fill in if dont have middle name

[A-Z9]{2}

3 letters and numbers which are puter digit check digits in the sequence:

\d and then [A-Z]{2}

This link explains the exact layout of a UK driving license.

http://webarchive.nationalarchives.gov.uk/20090203184459/http://www.direct.gov.uk/en/Motoring/DriverLicensing/DG_068315

Also see:

https://en.wikipedia/wiki/Driving_licence_in_the_United_Kingdom

The regex you have provided is to test against this format and will work against valid uk driving licenses but the rules you've set against it do not match the regex nor the format

^[A-Z9]{5}\d[0156]\d([0][1-9]|[12]\d|3[01])\d[A-Z9]{2}\d[A-Z]{2}$

本文标签: Validate UK Driving license regex in javascriptStack Overflow