I have Jtextfield, I want to format it, so that it should accept in the format below.
KYC123456L
Input will always start with "KYC" and ends with "L" and 6 numbers in between.
UI has a button that will copy the contents of other components and save it to a text file. But before it copies it should validate the jtextfield and copy if only format above matches, if not a message to be displayed.
Please suggest.
Frame the regex: ^KYC[0-9]{6}L$
to match the pattern stated in your question.
It'll match the strings starting with KYC, then 6 digits, and finally ending with L.
NOTE: ^
marks the beginning of the string, whereas $
marks the end of the string.
String patternString = "^KYC[0-9]{6}L$";
Pattern p = Pattern.compile(patternString);
String test = jTextField.getText();
Matcher m = p.matcher(test);
boolean matches = m.matches();
if(matches == true)
// allow
else
// JOptionpane.showMessageDialog ---> your desired error message.