How to format field for RG with issuing organ?

Asked

Viewed 2,066 times

3

I have the following java class:

public class TesteRG {

public static void main(String[] args) {
    String  RG = "24.77.195 ssp/pb";

    Pattern pattern = Pattern.compile("\\d{2}.\\d{2}.\\d{3}\\s\\w{3}/\\w{2}");
    Matcher matcher = pattern.matcher(RG);

    if (matcher.find()) {
        System.out.println("Valido");
    } else {
        System.out.println("Não Valido");
    }
}

It works, but when I call on the frame it has a jTextField called jTFRG with the following custom code "the problem is here":

jTFRG = new javax.swing.JFormattedTextField();
try {
    jTFRG.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.##.### ###/##")));
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}

it just lets me enter numbers. How do I insert a String equal to: 12.123.44 SSP/SP

The validation part is working now only need to insert the letters beyond the numbers.

When I try to insert the formatted field just let me put numbers and the String value gets: 11.11.111 111/11

  • I didn’t understand anything: your problem is with regex or formatted field??

  • @Diegof Formatted field

  • 1

    Just a hint, if the ID is not issued by the SSP, his number won’t hit this REGEX... Military for example, have some with 3R 28300 for example... or only 2300 and so on...

  • @Lucaseduardo Thanks for the valuable tip !! I hadn’t thought about it!! It’s true it can be issued by several organs. For now I’ll leave it like this, is there any Pattern design to solve this? Thanks!!

  • @Penapintada believe that there is not a Pattern for this yet. Up to pq each emitter has a numbering feature (can merge number and letter). What I see is a lot of system not checking if the RG is valid, only the CPF, my tip is that you validate only the CPF.

1 answer

4


According to class documentation MaskFormatter, the character # indicates the entry of numbers only, if you want to merge the numeric mask including letters, valid formatting characters are U(turn the typed letters into uppercase letters) and L(turns typed letters into minuscules):

Try the below:

 MaskFormatter formatter = new MaskFormatter("##.##.### UUU/UU");
 formatter.install(jTFRG);

I made a test with this mask and see the result:

inserir a descrição da imagem aqui

Only accept numbers in the first 7 digits, then only accept letters and convert to uppercase.

  • 1

    Thank you very much! Dude , already says the "hurry is the enemy of perfection"! I looked everywhere on the internet! Except in the documentation!!!

  • 1

    @Penapintada happens, sometimes I also break my head and the problem was there in my face, the business is to consult a second opinion or leave the code for a while with the coolest head kkk

  • @Penapintada which question? Any problems with Mask??

  • 2

    I will ask the question formally and then you answer in stackoverflow!

Browser other questions tagged

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