I want a JTextField
to accept only my specified pattern. "12ABCDE1234A1A1"
NOTE- that pattern can have any digit and alphabet.
JTextField t17 = new JTextField();
final String match ="(\\d\\d)-([a-zA-z]{5})-(\\d\\d\\d\\d)-([a-zA-z]{1})-
(\\d)-([a-zA-z]{1})-(\\d)+$";
Pattern pt = Pattern.compile(match);
final Matcher mt =pt.matcher(t17.getText());
t17.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent arg0) {
if(mt.matches()){
lblPin.setText("verified");
}else{
JOptionPane op = new JOptionPane();
op.showMessageDialog(iff, "INVALID GST NUMBER");
}
}
});
After you enter tekst to your JTextField
, validate it's text against this pattern:
^\\d{2}[A-Z]{5}\\d{4}([A-Z]\\d){2}$
And call t17.getText()
inside FocusAdapter
method, so you'll get currently entered text.
You can try this in Java code:
public class JavaFiddle
{
public static void main(String[] args)
{
java.util.regex.Pattern pt = java.util.regex.Pattern.compile("^\\d{2}[A-Z]{5}\\d{4}([A-Z]\\d){2}$");
java.util.regex.Matcher mt = pt.matcher("12ABCDE1234A1A1");
if(mt.matches()){
System.out.println("success");
}else{
System.out.println("failuire");
}
}
}
Okay, i solved the above problem using JformattedtextField.
MaskFormatter mask = new MaskFormatter ("##?????####?#?# "): JformattedtextField tf = new JformattedtextField (mask);
That's all.. no need of if else
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.