By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,600 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,600 IT Pros & Developers. It's quick & easy.

disable ctrl-v (paste)

P: n/a
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"
onKeyUp = "fncKeyStop();"> </form>
<script>
function fncKeyStop(){
if (window.event.ctrlKey){
if (window.event.keyCode == 86) {
document.myForm.myText.value = ""
}
}}
</script></body></HTML>

Chris
Jul 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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)
Jul 20 '05 #2

P: n/a
In comp.lang.javascript, chirs wrote:
I have a code to disable ctrl-v (paste) on the 2nd box.


Presumably this is to stop users from pasting their email address having
typed it once. If so, may I say how annoyed I would be as a user.
--
Nige

Please replace YYYY with the current year
ille quis mortem cum maximus ludos, vincat
Jul 20 '05 #3

P: n/a
Michael Winter hu kiteb:
type = content-type [p.53] [CI] [p.49]
This attribute specifies the scripting language of the element痴
contents and overrides the default scripting language. The =========================== scripting language is specified as a content type (e.g.,
"text/javascript"). Authors must supply a value for this attribute.
There is no default value for this attribute. ===================================

I think this specification could do with some proof-reading if your
interpretation is correct.
<META http-equiv="Content-Script-Type" content="text/javascript">


Does this replace the TYPE attribute in the SCRIPT tag?

--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #4

P: n/a
"Fabian" <la****@hotmail.com> writes:
Michael Winter hu kiteb:
type = content-type [p.53] [CI] [p.49]
This attribute specifies the scripting language of the element
contents and overrides the default scripting language. The

===========================
scripting language is specified as a content type (e.g.,
"text/javascript"). Authors must supply a value for this attribute.
There is no default value for this attribute.

===================================

I think this specification could do with some proof-reading if your
interpretation is correct.


Not really.

There is a "default scripting language" for a page. It can be set
with the META tag shown below. It is the default scripting language
for all scripts on the page.

There is no default *value* for the script tag's type *attribute*.
Furthermore, the type attribute is required, so in practice, the
default scripting language doesn't apply to script elements.

The "default scripting language" and the "type attribute" of script
tags are completely independent. The latter has no default value.
<META http-equiv="Content-Script-Type" content="text/javascript">


Does this replace the TYPE attribute in the SCRIPT tag?


No. It's not about script tags at all. Script tags require the type
attriubte. The intrinsic event attributes (e.g., "onclick") have no
type attribute, and they use the default scripting language of the
page.

Intrinsic events:
<URL:http://www.w3.org/TR/html4/interact/scripts.html#events>
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

P: n/a
JRS: In article <lb********************************@4ax.com>, seen in
news:comp.lang.javascript, Nige <uY***@ntlworld.com> posted at Fri, 28
Nov 2003 10:22:38 :-
In comp.lang.javascript, chirs wrote:
I have a code to disable ctrl-v (paste) on the 2nd box.


Presumably this is to stop users from pasting their email address having
typed it once. If so, may I say how annoyed I would be as a user.


It is a password confirm box, AFAICS, which means that using ^V rather
defeats the object of having a confirmation.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #6

P: n/a
Dr John Stockton hu kiteb:
JRS: In article <lb********************************@4ax.com>, seen in
news:comp.lang.javascript, Nige <uY***@ntlworld.com> posted at Fri, 28
Nov 2003 10:22:38 :-
In comp.lang.javascript, chirs wrote:
I have a code to disable ctrl-v (paste) on the 2nd box.


Presumably this is to stop users from pasting their email address
having typed it once. If so, may I say how annoyed I would be as a
user.


It is a password confirm box, AFAICS, which means that using ^V rather
defeats the object of having a confirmation.


In which case, shouldnt the form object be of type=password? iirc, that
type has cut and paste functions disabled as a 'security' measure.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #7

P: n/a
Fabian wrote on 28 Nov 2003:
Michael Winter hu kiteb:
type = content-type [p.53] [CI] [p.49]
This attribute specifies the scripting language of the
element痴 contents and overrides the default scripting
language. The

===========================
scripting language is specified as a content type (e.g.,
"text/javascript"). Authors must supply a value for this
attribute. There is no default value for this attribute.

===================================

I think this specification could do with some proof-reading if
your interpretation is correct.


It's not an interpretation, that is quoted verbatim. The only edited
sections are the descriptions of the src and defer attributes (which
I removed completely).
<META http-equiv="Content-Script-Type"
content="text/javascript">


Does this replace the TYPE attribute in the SCRIPT tag?


The type attribute is required. That why the specification says (as
you quoted!): "Authors must supply a value for this attribute." The
Content-Script-Type META element or HTTP header is used when parsing
intrinsic events. It is explained in section 18.2.2, Specifying the
scripting language, of the HTML 4.01 specification.

Mike

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #8

P: n/a
JRS: In article <bq*************@ID-174912.news.uni-berlin.de>, seen in
news:comp.lang.javascript, Fabian <la****@hotmail.com> posted at Sat, 29
Nov 2003 09:07:57 :-
Dr John Stockton hu kiteb:
It is a password confirm box, AFAICS, which means that using ^V rather
defeats the object of having a confirmation.


In which case, shouldnt the form object be of type=password? iirc, that
type has cut and paste functions disabled as a 'security' measure.


You do not RC! Copy and Cut are disabled, but Paste works, in my
browser, in a Password box. But it seems likely that it should be
type=password.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #9

P: n/a
chirs wrote:
I have a code to disable ctrl-v (paste) on the 2nd box.

[snip]

How do you know that Ctrl-V is the shortcut for Paste? You could be
disabling some other completely different and important feature of the
browser.

Jul 20 '05 #10

P: n/a
David Leverton <u0*****@abdn.ac.uk> writes:
chirs wrote:
I have a code to disable ctrl-v (paste) on the 2nd box.

[snip]

How do you know that Ctrl-V is the shortcut for Paste? You could be
disabling some other completely different and important feature of the
browser.


E.g., it probably also disables Ctrl-Alt-V, which has a function in
Opera. Not one that you would probably want the user to use, though,
as it sends the page to an HTML validator :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.