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">
@Maniero why closed the topic?
– João Pimentel Ferreira
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
@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.
– João Pimentel Ferreira
I didn’t give negative, I just closed.
– Maniero