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??
– user28595
@Diegof Formatted field
– Pena Pintada
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...
– Lucas Eduardo
@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!!
– Pena Pintada
@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.
– Lucas Eduardo