-2

I want to validate if a plate of a Portuguese car has a valid structure.

Portuguese Plate is XX-XX-XX alpha numeric. It's Alpha numeric but you can´t have a number and a Char on the same position Example :

A5-99-AB -> It's wrong

55-99-AB -> it's right

Although there are sequences that are not yet valid according to PT plate rules (e.g 2 sets of letters AA-00-AA) this is something that I do not pretend to validate since it changes often.

Carlos Sá
  • 602
  • 3
  • 10
Capitche
  • 5
  • 1
  • 2
    Hello and welcome. You forgot to add a question, what you tried and where it failed or where you're stuck. – Federico klez Culloca Nov 22 '19 at 11:32
  • You should try regex, here's a fellow user who had the same problem https://stackoverflow.com/questions/25345341/how-can-i-match-portuguese-license-plates-in-a-single-regular-expression always check for duplicates – Carlos Sá Nov 22 '19 at 11:32
  • @CarlosSá it is not duplicate - this question gives more details about Portuguese Plates structure – Kamil Kiełczewski Nov 22 '19 at 11:39
  • 1
    What detail does it give that the other does not? The linked question literally lists all the valid structures – Carlos Sá Nov 22 '19 at 11:40
  • @CarlosSá question in your link describes that plates can have only two letters - in this question the 6 letters are also allowed – Kamil Kiełczewski Nov 22 '19 at 11:41
  • Possible duplicate of [How can I match Portuguese license plates in a single regular expression?](https://stackoverflow.com/questions/25345341/how-can-i-match-portuguese-license-plates-in-a-single-regular-expression) – AD7six Nov 22 '19 at 11:45
  • Then according to portuguese license plate rules it would be invalid, we (I'm portuguese) only have (at the moment) 1 pair of letters per license plate (and it must be coupled together). 4 letters are predicted to be in use in early 2020 but it's still invalid. – Carlos Sá Nov 22 '19 at 11:45
  • Well that's messy. There's an existing question for this which is currently closed https://stackoverflow.com/questions/25345341/how-can-i-match-portuguese-license-plates-in-a-single-regular-expression. There should be only one question for this, with one canonical/accepted answer. – AD7six Nov 22 '19 at 11:47
  • @AD7six - but question in your link allows only 2 letters - but this question allows 6 letter - this changes the solution. Im not the lawyer - however I don't know OP the purpose and why for he need this (may be he implement some system which will be used after 2020...). – Kamil Kiełczewski Nov 22 '19 at 11:48
  • @KamilKiełczewski "I want to validate if a plate of a Portuguese car is valid." this is the OP's objective, and since 6 letters would make the plate invalid it's the same solution as the linked question – Carlos Sá Nov 22 '19 at 11:50
  • @KamilKiełczewski - requirements change with time, but the question is the same "How can I validate a PT number plate?" - this is a duplicate. – AD7six Nov 22 '19 at 11:50
  • @CarlosSá - i check 'AA-00-AA' for checked solution in your link question - an it is false - so this solution not meet requirements of this question – Kamil Kiełczewski Nov 22 '19 at 11:52
  • @AD7six - but answer for this two questions are different - so questions are not similar. The old question probably is not "2020 ready" – Kamil Kiełczewski Nov 22 '19 at 11:53
  • @KamilKiełczewski and it's rightfuly false, AA-00-AA is not yet adopted as a valid sequence – Carlos Sá Nov 22 '19 at 11:54
  • 1
    If you understand the questions to be different @KamilKiełczewski _edit the question so that it is actually different_ please. – AD7six Nov 22 '19 at 11:54
  • 1
    @KamilKiełczewski that's a disappointing edit. Why are you limiting the question to only be understood by people aware of what is going to change in 2020 instead of explaining it? I am still of the opinion this question is a duplicate of https://stackoverflow.com/questions/25345341/how-can-i-match-portuguese-license-plates-in-a-single-regular-expression because _the question asked is the same_. – AD7six Nov 22 '19 at 11:58
  • @AD7six - I dont get what do you mean - just do it yourself - in my opinion question has all informations (based on examples) – Kamil Kiełczewski Nov 22 '19 at 12:00
  • The validate it's good now but i want to put an auto hypen "-" ever 2 chars, in regex how can i do that? – Capitche Nov 25 '19 at 10:27

1 Answers1

0

Try

let re = /^([A-Z]{2}|\d\d)-([A-Z]{2}|\d\d)-([A-Z]{2}|\d\d)$/;

console.log( re.test('A5-99-AB') );
console.log( re.test('55-99-AB') );
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
  • This should be an answer on the duplicate question - with a note explaining that PT number plate requirements changed (as of date). The question should also be updated to reflect that - a reference https://stackoverflow.blog/2011/06/13/optimizing-for-pearls-not-sand/ – AD7six Nov 22 '19 at 11:51
  • @AD7six the duplicate question for which you provide link in comments below OP question - is CLOSED and you can't add any new answer – Kamil Kiełczewski Nov 22 '19 at 12:03
  • Which is why I voted to REOPEN it. There's an existing question, with existing relevant answers - and this question is a duplicate of that question. I don't see the OP making any mention of wanting to support the future format btw. – AD7six Nov 22 '19 at 12:04