TextField to avoid special characters to be entered

Hi All

I need to have a text-field which shows validation messages on entering special characters such. How can i do that? I used the following method to implement this but its the message is not clearing on entering right text. Is there any other way i can do this?

if (! ((String) textfield.getValue()).matches(“^\w*$”)) {
// Put the component in error state and
// set the error message.
new UserError(“Must be letters and numbers”));


Here are two options:

  • Add a ValueChangeListener to the field and check the value. Als call setImmediate(true) on the field
  • There is also a client side validation addon which could be the fastest
  • Add a RegexpValidation to the field the set the Form to writeThrough = true (if your using a form) (not directly validates, but after leaving the field)

Or use AutoReplaceField with a proper regexp and your users cannot even type those special letters.

http://vaadin.com/directory#addon/autoreplacefield (set the demo)


nice addon, did not know it :slight_smile:

The addson has a problem when we copied string with ctrl+v and the field, support only integer