0

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.

1 Answer 1

3

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. 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.