How to disable paste from key mobile keyboard for one-time passcodes

181 views Asked by At

This below method will prevent paste when long pressing a html input field and pressing paste. However, in the case of sms one time passcode, the user can still paste the code from the top of the keyboard. How can this be prevented?

window.onload = () => {
    const myInput = 
    document.getElementById('myInput');
    myInput.onpaste = e => 
    e.preventDefault();
}
1

There are 1 answers

1
SHAHEEREZ On

You can disable paste in your input as follows:

HTML:

 <input type="text" id="myinput" onpaste="return false;" ondrop="return false;" autocomplete="off" />

Javascript:

window.onload = () => {
const myInput = document.getElementById('myInput');
myInput.onpaste = e => e.preventDefault();
}

also try

myElement.addEventListener('paste', e => e.preventDefault());