How in Java I can create a field that can accept only digits and once dot?
NickName:Alex K Ask DateTime:2015-05-11T20:22:17

How in Java I can create a field that can accept only digits and once dot?

I look all over the net and all the things I tried didn't work....

I found that I can use some JTextFormatterField but it didn't work.

Then I found that I can use DocumentFilter with regex and that what I did:

JTextField jFormattedTextFieldMoneyToConvert = new JTextField();
    ((AbstractDocument) jFormattedTextFieldMoneyToConvert.getDocument()).setDocumentFilter(new DocumentFilter(){
        Pattern regEx = Pattern.compile("^\\d+\\.?\\d*$");

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            Matcher matcher = regEx.matcher(text);
            if (!matcher.matches()) {
                return;
            }
            super.replace(fb, offset, length, text, attrs);
        }
    });

but it doesn't work...It accepts only digits. I want it to accept also dots. And I also need it to not to start with a dot and not to end with a dot.

what I'm doing wrong?

Copyright Notice:Content Author:「Alex K」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30167524/how-in-java-i-can-create-a-field-that-can-accept-only-digits-and-once-dot

Answers
Bohemian 2015-05-11T12:50:46

This is probably the simplest code that works:\n\nif (text.matches(\"\\\\d+(\\\\.\\\\d+)?\"))\n\n\nIf there's a dot, it must be followed by some digits. If you want to allow a dot then no digits too, change the + to *:\n\nif (text.matches(\"\\\\d+(\\\\.\\\\d*)?\"))\n\n\nNote that with String#matches(), the leading/trailing ^/$ are not necessary as they are implied, because the entire string must be matched for matches() to return true.\n\nAllowing no leading digits too becomes trickier, because the regex (\\d+)?(\\.\\d*) matches the blank string, so you need an alternation:\n\nif (text.matches(\"\\\\d+(\\\\.\\\\d*)?|\\\\.\\\\d+\"))\n\n\nor a negative look ahead:\n\nif (text.matches(\"(?!$)\\\\d*(\\\\.\\\\d*)?\"))\n\n\nThe little look head asserts that the next position after the start is not the end (ie input is not blank).",


More about “How in Java I can create a field that can accept only digits and once dot?” related questions

How in Java I can create a field that can accept only digits and once dot?

I look all over the net and all the things I tried didn't work.... I found that I can use some JTextFormatterField but it didn't work. Then I found that I can use DocumentFilter with regex and that

Show Detail

jTextField accept only one dot in java calculator?

I have created a simple calculator in java using net beans. In this calculator when user accidentally press dot button(jButton) multiple time textfield shows many dots as user pressed. for example if

Show Detail

Formatting JTextField to accept three digits at most, but anything up to 1-3 digits can be typed

I have the following JFormattedTextField try { MaskFormatter CabinNoTextF = new MaskFormatter("###"); CabinNoTextF.setPlaceholderCharacter(' '); CabinNoTextField = new JFormattedTextFi...

Show Detail

JTextField accept only numbers and one dot

I want that my text field accept only numbers (digit) and one dot, because it is a field in which the user can write the price of products. I have this code but it doesn't work well, it only accept

Show Detail

How I can limit the introduced digits in the cell of the datagridview?

I want to create a datagridview that only accept numbers (Integers or doubles). When the number is double, I only want to save 2 digits after the point. (E.g. 1.22) and automatically change the cel...

Show Detail

Email validation with validation on username to accept only letters, digits, underscore and DOT

I know this question asked many times but I could not find any solution to my specific validation. I want to validate a username in email address to accept only letters, digits, underscore and DOT...

Show Detail

jQuery validate : accept one or two digits after dot

I use this custom validator for money fields, it accepts $ sign, and dot or comma. But it's not perfect : $.validator.addMethod("money", function(value, element) { return this.optional(element...

Show Detail

Accept only numbers and a dot in Java TextField

I've got one textField where I only accept numbers from the keyboard, but now I have to change it as it's a "price textField" and I would also need to accept a dot "." for any kind of prices. How ...

Show Detail

Accept only numbers and a dot in Java TextField

I've got one textField where I only accept numbers from the keyboard, but now I have to change it as it's a "price textField" and I would also need to accept a dot "." for any kind of prices. How ...

Show Detail

jquery validation only digits

I am having the jquery.validate.js plugin and it is working fine for me. My question is : I am having a textbox and this textbox is mandatory and should only accept digits. In the js, I can see ...

Show Detail