Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
100
rated 0 times [  106] [ 6]  / answers: 1 / hits: 71389  / 11 Years ago, sun, march 10, 2013, 12:00:00

I have this JS function that prevents user from typing characters



<script type=text/javascript>
function validate(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode(key);
var regex = /[0-9]|./;
if(!regex.test(key)) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
</script>

<span>Radio Receive:</span>
<input name=ReceiveNo type=text class=txtbox onkeypress='validate(event)' maxlength=11 value=${cpCon.receiveNo} required tabindex=34 />


But I noticed that when the user tried to paste a word from this textbox, the text can be entered. How can I prevent this without disabling the paste?


More From » html

 Answers
22

Very Easy Solution:



<input name=ReceiveNo type=text class=txtbox onkeypress='validate(event)' maxlength=11 value=${cpCon.receiveNo} required tabindex=34 onCopy=return false onDrag=return false onDrop=return false onPaste=return false autocomplete=off />


This worked for me very well. No one can paste now into your textbox using right button paste option of mouse or pressing ctrl+v from the keyboard.


[#79698] Friday, March 8, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hallie

Total Points: 503
Total Questions: 114
Total Answers: 103

Location: Iraq
Member since Fri, Jun 5, 2020
4 Years ago
;