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
Asked
Active
Viewed 254 times
2
-
Olivers example works great for EmailField just like TextField. However I would recommend to add his solution to both fields. Suppose the user types wrong e-mail in first field but correct in second. Without "anti-copy" on both fields the user may copy the correct value from field2 into field1. I would probably even extend EmailField and add Olivers solution and just use that new field for an even cleaner solution. – Avec Feb 18 '22 at 11:25
-
I really appreciate your advice, I will apply it. Thanks a lot – wilson ramos Feb 18 '22 at 13:43
-
Welcome to SO! Please take the [tour] and read "[ask]", "[Stack Overflow question checklist](https://meta.stackoverflow.com/questions/260648)", "[mre]" and their linked pages. We'd like to know what you tried to solve this. Without that we have to write a tutorial or give you generalized answers. – the Tin Man Mar 10 '22 at 04:40
1 Answers
4
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");

Oliver Stahl
- 617
- 3
- 19