Text Field input method

Hi all

Is it possible to have a Text field to which only Numerals or only Alphabets can be entered? If i type Alphabets in a text field which accepts only numerals it should not accept the Alphabets. Is it possible to implement this functionality?

Thank you

Hi,

If you wish to reject invalid text (letters in a numeric field),
look at using validators
.

If you wish to prevent invalid text being entered, there is currently nothing in the core of Vaadin - however, the
MaskedTextField add-on
can be used to do this.

Cheers,

Charles.

Thank you for the reply. I had already used validators. But just i wanted to know if there was such functionality. Anyway thanks Charles

To annswer your question : I don’t think so.

At work, we had the same brainstorming few weeks ago for a textField who suppose to accept only Integer.

We did not want the use the Vaadin validator, so we override the setValue method of the textField to accept only “castable” in Intger.

If it’s “castable” then we call super.setValue(newValue).
If not, we do super.setValue(null) and we display a notification to the user.

But your best choice is, of course, the validator.

Regards.

Éric

Could you just have the validator change the value of the field and then acknowledge that it’s valid?

I know that’s not exactly how validators are supposed to work, but you ought to be able to run any code you want in one.

Cheers,
Bobby

The problem with this is that validators are (also) run during the paint phase, and at that point you should not change values. Any changes made in that phase might not have their effects propagate all the way to other components before the components are painted etc.

Thanks! Now I know not to misuse validators.

Cheers,
Bobby

H iEric,

i’m tried to use that solution with following code on text field:

	@Override
	public void setValue(Object newValue) throws ReadOnlyException, ConversionException {
		Object result = newValue;

		if (newValue instanceof String) {
			try {
				Integer.parseInt((String) newValue);
			} catch (Exception ingore) {
				result = null;
			}
		}

		super.setValue(result);
	}

But i can type the Alphabets into field, and those will be displayed

Hey Ranjit,

textField.addValidator(new Validator() {
  @Override
  public void validate(Object value) throws InvalidValueException {
    // Check for alphanumeric values only.
  }
});

To check if a Java String only contains alphanumeric values, one of these should help:


  1. http://stackoverflow.com/questions/8248277/how-to-determine-if-a-string-has-non-alphanumeric-characters

  2. http://stackoverflow.com/questions/12831719/fastest-way-to-check-a-string-is-alphanumeric-in-java

I have created a component for that.

package sandbox;

import com.vaadin.data.Validator;
import com.vaadin.ui.TextField;

public class AlphaNumericField extends TextField {

    public AlphaNumericField() {
        super();
        addValidator();
    }
    
    public AlphaNumericField(String label) {
        super(label);
        addValidator();
    }
    
    private void addValidator() {
        this.addValidator(new Validator() {

            @Override
            public void validate(Object value) throws InvalidValueException {
                if(!((String) value).matches("[A-Za-z0-9]
+"))
                    throw new InvalidValueException("Only AlphaNumeric!");
            }
            
        });
    }
    
}

And add instance on UI.


Dear Sir,
I am a beginer of Vaadin, I have some problem which is explained below.

I am trying to make an autofill web application in which there are two filels User Name TextField and second one is Password Password Field and one Submit Button.
I wish when I filled the username and password fields and Click on submit Button then the value of User Name Text Field and Password fields will send to and external website “www.xyz.com” which has also two fields User Name and Password.
Means after clicking the submit button on my page ,User Name value of TextField and Password field value will be send to the User Name text Fields and Password Fields respectively of external wesite page"www.xyz.com"

Please help me…
Thank in advance.

Rajesh,

try use FormSender
https://vaadin.com/directory#addon/formsender