Mercosul standard vehicle plate?

Asked

Viewed 3,126 times

0

I’m working on a project and I don’t know how to do it. As in the statement, I need to validate the plates with the Mercosul standard, I have a method to validate the old plates of vehicles.

public boolean validaPlaca(String placa) {
    boolean result = false;

    Pattern pattern = Pattern.compile("[A-Z]{3}-\\d{4}");
    Matcher mat = pattern.matcher(placa);
    if (!mat.matches()) {
        result = false;
    } else {
        result = true;

    }
    return result;

}

Now to validate the standard Mercosur plate do not know how to do. I’ve done:

Pattern pattern = Pattern.compile("[A-Z]{3}\d{1}[A-Z]{3}\d{2}");

And also:

Pattern pattern = Pattern.compile("[A-Z]{3}[0-9][A-Z][0-9]{2}");

None worked, remembering that the Mercosur standard is AAA3B11. The first 3 digits are letters, the fourth is a number, the fifth a letter and the sixth and seventh a number.

  • What is "didn’t work"? Gave an error? Which formats passed validation and should not have passed?

  • 3

    Utilize ^[A-Z]{3}\\d[A-Z]\\d{2}$. Example: https://ideone.com/Vq8yi8

  • Opa blz my friend, in fact he does not check, passes straight..... or is accepted anything

  • Are you sure the problem isn’t at another point in the code? Because the regex is right: https://ideone.com/e97vyf - Maybe the improvement suggested by Valdeir helps, because it ensures that string only has the board and nothing else. And you can also greatly simplify your method validaPlaca, see: https://ideone.com/WMwsKW

3 answers

6


Note that your method is validatePlate, therefore should validate the old plates (which will still be in circulation) and also the new plates in the Mercosul model.

So your regex should validate both formats. An expression to validate both formats at the same time would be this: [A-Z]{3}[0-9]{1}[A-Z]{1}[0-9]{2}|[A-Z]{3}[0-9]{4}

But there are still considerations to do, for example, the plate is of the Mercosur standard but each country has a combination of different letters and numbers.

For example:

  • Argentina: AB123CD
  • Brazil: ABC1D23
  • Uruguay: ABC1234
  • Paraguay: 123ABCD

In summary, would adopt the same regular expression that the IRS does in the issuance of Electronic Tax Documents (NF-e, CT-e and MDF-e) that is this: [A-Z]{2,3}[0-9]{4}|[A-Z]{3,4}[0-9]{3}|[A-Z0-9]{7}, this way would not be restricted the validation of the plates of Brazil.

  • 1

    Obg Lgregianin, worked perfect vlw

  • @Wesleysouza, marks the answer as a solution to the question.

1

Well, from what I can see the second regex that you exemplified is correct "[A-Z]{3}[0-9][A-Z][0-9]{2}".

I believe then that the error is in the function that validates the plate and uses this regex.

Try using the function below to see if it works, so I’ve tested it right here.

public boolean validarPlaca(String placa) {
    boolean placaValida = true;

    if (placa.length() > 0) {
        if (placa.length() < 7) {
            placaValida = false;
        } else {
            if (!placa.matches("[A-Z]{3}[0-9][A-Z][0-9]{2}")) {
                placaValida = false;
            }
        }
    }

    return placaValida;
}

Tip: Use the site Regex101 to check whether or not their regex are right.

Source: Mercosur plate - Alberto Marianno

0

Follow my check function:

if (txtPlaca.getText().isEmpty()) {
    ValidationFields.checkEmptyFields(txtPlaca);
    Alert dlg = new Alert(AlertType.ERROR);
    dlg.setContentText("Preencha o campo PLACA!!!");
    dlg.showAndWait();
    txtPlaca.requestFocus();
    return;
} else {
    boolean valida = true;
    String validar = txtPlaca.getText();
    System.out.println("validar placa : " + validar);

    if (validaPlaca(validar) == valida || validaPlacaMercosul(validar) == valida) {

    } else {

        Alert dlg = new Alert(AlertType.ERROR);
        dlg.setContentText("Placa digitada não e válida!!!");
        dlg.showAndWait();
        txtPlaca.requestFocus();
        return;
    }

    placa = txtPlaca.getText();

}
  • 1

    Is this a solution to your question? If it’s not, it’s not the right place to post. You must add additional codes to the question itself by clicking EDIT/

  • Values boolean do not need to be compared with ==, just do if (validaPlaca(validar) || validaPlacaMercosul(validar))

Browser other questions tagged

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