Forums Register Login

Regex constraint on a textfield

+Pie Number of slices to send: Send
Hello, all.

I want to put a constraint on a JFormattedTextField that will only allow characters matching a regex to be entered (in this case, only hex digits 0-E).

The possible solutions I've found to this are all quite complicated. I know I've done it before and it was simple, but having trouble remembering.
+Pie Number of slices to send: Send
Jess,
Welcome to JavaRanch!

This character class matches hex: [0-9A-E]

What else are you looking for that was complicated?
+Pie Number of slices to send: Send
I was wondering how to add that as a constraint to a textfield, so that the user can only enter those characters.
+Pie Number of slices to send: Send
Use MaskFormatter. See the doc of the class for its usage.
+Pie Number of slices to send: Send
Thank you, Hiroshi! MaskFormatter did exactly what I needed.
Hey! Wanna see my flashlight? It looks like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1125 times.
Similar Threads
Filter only GET Methods
http-method
security constraint : http-method
HfSJ 637 page Errata
Amout validation
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:27:15.