Search code examples
vaadinvaadin-flowvaadin22

How can I block copy-paste in a text field?


I'm using vaadin 22 and I want to block the copy-paste of an emailField to implement email confirmation. Can someone help me. Thank you


Solution

  • In HTML you want to archive something like this:

     <input type="textbox" onpaste="return false;" oncopy="return false" oncut="return false" ondrag="return false" ondrop="return false">
    

    Demo here: https://jsfiddle.net/f4g6opcm/1/

    So you can simply add this Attributes via Vaadin like this:

    TextField textfield = new Textfield();
    textfield.getElement().setAttribute("onpaste", "return false");
    textfield.getElement().setAttribute("oncopy", "return false");
    textfield.getElement().setAttribute("oncut", "return false");
    textfield.getElement().setAttribute("ondrag", "return false");
    textfield.getElement().setAttribute("ondrop", "return false");