Which Regexp to validate Portuguese registrations

Asked

Viewed 572 times

-1

I wonder what the Regular Expression validating registrations of Portugal.

whereas there are such registrations in Portugal

  • AA-00-00
  • 00-00-AA
  • 00-AA-00
  • AA-00-AA

what Regexp code I have to use to validate only such registrations on a form?

<input id="matrícula" type="text">
  • @Maniero why closed the topic?

  • Because we don’t answer questions that give a statement and wait for them to make the code for you. The questions should only be conceptual or have a specific problem. This is what is called enunciation, or do it for me. It makes no sense to close when a person asks and keep open when the person asks and answers.

  • @Maniero was right, but he didn’t want to give a bad point. I asked the question and answered because I thought this question was relevant and I wanted to share the conclusions I reached.

  • I didn’t give negative, I just closed.

1 answer

2


According to the Wikiedia

Initially the sequence used was AA-00-00. In 1992 it was changed to 00-00-AA and in 2005 to 00-AA-00 which is currently in force. When it comes to an end, the introduction of the sequence AA-00-AA is planned.

That is, we consider the four formats valid

  • AA-00-00
  • 00-00-AA
  • 00-AA-00
  • AA-00-AA

Just now consider the following regular expression that considers exactly these 4 valid cases, with a common hyphen (-) separating the digits

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

Here’s an example you can use

$("#matricula").css('outline','none').on('input', function(e){
  e.preventDefault()

  var expr = /^(([A-Z]{2}-\d{2}-(\d{2}|[A-Z]{2}))|(\d{2}-(\d{2}-[A-Z]{2}|[A-Z]{2}-\d{2})))$/

  if (!expr.test($(this).val())){
    $(this).css('border', '2px solid red')
  } else {
    $(this).css('border', '2px solid green')
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Insira a matrícula, usando o hífen comum (-) entre os dígitos:<br>
<input id="matricula" type="text">

  • 1

    I suggest putting the ^ in the beginning, otherwise regex will accept values such as "AAA-00-AA". Also, you can simplify a little: \d amounts to [0-9] and you can group some cases (if you start with letters, you always have 2 numbers after and at the end 2 numbers or letters, that is, [A-Z]{2}-\d{2}-(\d|[A-Z]){2})). And if it starts with numbers, the rest is 2 numbers and 2 letters or 2 letters and 2 numbers (ie, \d{2}-(\d{2}-[A-Z]{2}|[A-Z]{2}-\d{2})). Then the regex would be ^(([A-Z]{2}-\d{2}-(\d|[A-Z]){2})|(\d{2}-(\d{2}-[A-Z]{2}|[A-Z]{2}-\d{2})))$ - see

  • 1

    Correction, in the first case (starts with letters), would be [A-Z]{2}-\d{2}-(\d{2}|[A-Z]{2}) (the previous comment version accepted a number and a letter at the end, now accepts only 2 numbers or 2 letters). So the regex is ^(([A-Z]{2}-\d{2}-(\d{2}|[A-Z]{2}))|(\d{2}-(\d{2}-[A-Z]{2}|[A-Z]{2}-\d{2})))$ - see. Another detail is that if you use the bars, you don’t need to call the constructor RegExp, that is, it may just be var expr = /^(([A-Z]{2}.../

  • @hkotsubo well seen, I edited accordingly as suggested

Browser other questions tagged

You are not signed in. Login or sign up in order to post.