chirs wrote on 28 Nov 2003:
I have a code to disable ctrl-v (paste) on the 2nd box. The
problem is that when I type ctrl-v, the text shows, then
disappear after I release ctrl-v. How can I make it not to show
in the box. In VB, I can set keyascii=0 to kill the input. Is
there a similar way to do it in JavaScript? Thanks a lot.
<HTML><head></head><body>
<form name="myForm">
Password: <input type="text"><br>
Comfirm: <input type="text" name="myText"
Typo: Comfirm -> Confirm
^ ^
onKeyUp = "fncKeyStop();"> </form>
<script>
The type attribute is mandatory. This should read:
<SCRIPT type="text/javascript">
function fncKeyStop(){
if (window.event.ctrlKey){
if (window.event.keyCode == 86) {
document.myForm.myText.value = ""
You should access forms and form elements using their respective
collections:
document.forms['myForm'].elements['myText'].value = "";
}
}}
</script></body></HTML>
Use both the onkeyup and onkeydown events. That way, it will fire
when a key is pressed once, and when it's released (if held long
enough for the keystroke to repeat).
You should also change the function content to:
function fncKeyStop() {
// Check if the control key is pressed.
// If the Netscape way won't work (event.modifiers is undefined),
// try the IE way (event.ctrlKey)
var ctrl = typeof event.modifiers == 'undefined' ?
event.ctrlKey : event.modifiers & Event.CONTROL_MASK;
// Check if the 'V' key is pressed.
// If the Netscape way won't work (event.which is undefined),
// try the IE way (event.keyCode)
var v = typeof event.which == 'undefined' ?
event.keyCode == 86 : event.which == 86;
// If the control and 'V' keys are pressed at the same time
if ( ctrl && v ) {
// ... discard the keystroke and clear the text box
document.forms['myForm'].elements['myText'].value = '';
return false;
}
return true;
}
....and the intrinsic event bodies to:
<INPUT ... onkeyup="return fncKeyStop()"
onkeydown="return fncKeyStop()">
This works in Internet Explorer and Opera. It should hopefully work
in Mozilla and Netscape too, but I can't test them.
Hope that helps,
Mike
--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)