Auto-trim most user input

Is there a way to auto-trim most user input so that leading/trailing whitespace is removed?

I have TextFields, for example, where I also have a field validator, but that validator may fail if there were trailing spaces. I’d like to have the textfield trim the input before it goes through converters/validators. Is this possible? Rarely are such spaces wanted in a typical data input app.

Is there a straightforward way to accomplish this? In the JSP/servlet days, our ‘getParam’ routines would do such stripping for us – and some were coded to remove unwanted input that included script/object/etc.

The best would be a simple method on TextField (or other text input capabilities, such as comboboxes) to request trim, like setTrimInput(true).

This is not a solution, but it may be a clue. The replaceAll command, applied to a string allows a variety of regex-based substitutions, which you can fashion to your needs. I use it to ensure a textfield that contains only non-printable characters is treated as empty. regularText is a TextField. You could do something similar to identify and kill leading/trailing spaces. This would be easy if your text contains no internal spaces - just get rid of all spaces…

        // there is no use saving an empty field.  We'll take a broad view of "empty" here and define
        // empty as containing either null, or nothing except non-printable characters.        
        // replace a variety of non-printable characters (if any) in the message with nothing ("")
        String messageWithoutNonPrintables = ((String) regularText.getValue()).replaceAll("[^\\p{Print}]
", "");
        // replaces each space with nothing ("")
        String messageCleansed = messageWithoutNonPrintables.replaceAll(" ", "");
        // if there was nothing, or nothing but non-printable characters in the TextField,
        if ( messageCleansed.equals("") ) { 
            // just do nothing.

Thanks, Steve. It’s easy to manipulate the data once I retrieve it, but I’m talking about having this occur early so that it works on “text input” that is converted to other objects via Converters and before Validators kick in so that I don’t have to do the cleansing on most every field and then set it’s value back so that the user sees the cleansed value. Sometimes a leading/trailing space, in particular, are tricky because the user may not notice it.

Is there a hook in these input fields where I can do some sort of “pre-manipulation” before it’s sent to validators/converters and made available via TextField.getValue()?

You might be able to do this by creating an Extension for Textfield which catches the event on the client side and manipulates the Text before it gets send to the server side.

This is a couple of years late - but in case anyone searches and finds this post like I did.

I just added a BlurListener to my text field (in fact subclassed TextField) which trims the text on blur. This seems to run prior to any validators etc.

I did the following in my FieldFactory:

@Override protected <T extends AbstractTextField> T createAbstractTextField(Class<T> fieldType) { T textField = super.createAbstractTextField(fieldType); textField.setNullRepresentation(""); textField.addBlurListener(e -> { AbstractTextField c = ((AbstractTextField) e.getComponent()); if (c.getValue() != null) { c.setValue(c.getValue().trim()); } }); return textField; } That worked perfectly!