How to Disable right click both mouse and keyboard in MIcrosoft Edge - javascript

Disable right click in Microsoft Edge.This below code works fine in Google and Internet Explorer
document.onmousedown = clickfn;
function clickfn(e) {
var button;
if (navigator.appName == "Microsoft Internet Explorer") {
button = event.button;
}
else {
button = e.buttons;
}
if (button == 2) {
alert("Right Click Disabled");
if (navigator.appName == "Microsoft Internet Explorer") {
event.returnValue = false;
}
return false;
}
}
</script>

This code will disable mouse right-click and keyboard shortcuts as well in IE,edge,chrome,firefox
jQuery(document).ready(function() {
function disableSelection(e) {
if (typeof e.onselectstart != "undefined") e.onselectstart = function() {
return false
};
else if (typeof e.style.MozUserSelect != "undefined") e.style.MozUserSelect = "none";
else e.onmousedown = function() {
return false
};
e.style.cursor = "default"
}
window.onload = function() {
disableSelection(document.body)
};
window.addEventListener("keydown", function(e) {
if (e.ctrlKey && (e.which == 65 || e.which == 66 || e.which == 67 || e.which == 70 || e.which == 73 || e.which == 80 || e.which == 83 || e.which == 85 || e.which == 86)) {
e.preventDefault()
}
});
document.keypress = function(e) {
if (e.ctrlKey && (e.which == 65 || e.which == 66 || e.which == 70 || e.which == 67 || e.which == 73 || e.which == 80 || e.which == 83 || e.which == 85 || e.which == 86)) {}
return false
};
document.onkeydown = function(e) {
e = e || window.event;
if (e.keyCode == 123 || e.keyCode == 18) {
return false
}
};
document.oncontextmenu = function(e) {
var t = e || window.event;
var n = t.target || t.srcElement;
if (n.nodeName != "A") return false
};
document.ondragstart = function() {
return false
};
});

Related

Can't enter numbers using numbers pad

I have a function that formats user input into phone format.
It works fine except it's not allowing numbers from numbers pad at the right, Only the numbers at the top of the alphabetic characters.
I want to keep the same format, But allow entering numbers from numbers pad.
Here is a fiddle:
https://jsfiddle.net/s1wyrmk6
Here is the code:
HTML:
<input type="text" id="phone">
JS/jQuery:
(function ($) {
$.fn.usPhoneFormat = function (options) {
var params = $.extend({
format: 'xxx-xxx-xxxx',
international: false,
}, options);
if (params.format === 'xxx-xxx-xxxx') {
$(this).bind('paste', function (e) {
e.preventDefault();
var inputValue = e.originalEvent.clipboardData.getData('Text');
if (!$.isNumeric(inputValue)) {
return false;
} else {
inputValue = String(inputValue.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3"));
$(this).val(inputValue);
$(this).val('');
inputValue = inputValue.substring(0, 12);
$(this).val(inputValue);
}
});
$(this).on('keydown touchend', function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
return false;
}
var curchr = this.value.length;
var curval = $(this).val();
if (curchr == 3 && e.which != 8 && e.which != 0) {
$(this).val(curval + "-");
} else if (curchr == 7 && e.which != 8 && e.which != 0) {
$(this).val(curval + "-");
}
$(this).attr('maxlength', '12');
});
} else if (params.format === '(xxx) xxx-xxxx') {
$(this).on('keydown touchend', function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
return false;
}
var curchr = this.value.length;
var curval = $(this).val();
if (curchr == 3 && e.which != 8 && e.which != 0) {
$(this).val('(' + curval + ')' + " ");
} else if (curchr == 9 && e.which != 8 && e.which != 0) {
$(this).val(curval + "-");
}
$(this).attr('maxlength', '14');
});
$(this).bind('paste', function (e) {
e.preventDefault();
var inputValue = e.originalEvent.clipboardData.getData('Text');
if (!$.isNumeric(inputValue)) {
return false;
} else {
inputValue = String(inputValue.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3"));
$(this).val(inputValue);
$(this).val('');
inputValue = inputValue.substring(0, 14);
$(this).val(inputValue);
}
});
}
}
}(jQuery));
Change the following
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) { return false; }
The additional range 96 to 105 is greater than 57 currently, which causes this statement to catch the numpad.
To allow the numpad:
if (e.which != 8 && e.which != 0 && (e.which < 48 || (e.which > 57 && !(e.which>=96 && e.which<=105 )))) {
return false;
}

Disable all keys but copy paste combination

I am writing a code for number field where i have disabled all keys except number keys
function doValidation(event) {
var charCode = event.keyCode;
if (charCode != 190 && charCode != 40 && charCode != 39 && charCode != 38 && charCode != 37 && charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 96 || charCode > 105))
return false;
}
<input type="text" onkeydown="doValidation(event)">
Now i want to enable ctrl+c and ctrl+v in this funtion.
you can do this like below :)
var is_ctrl_pressed = false;
$('#number_input').on('keydown', function(e) {
var code = e.which;
if ((code > 47 && code < 59) || (code > 95 && code < 106) || (is_ctrl_pressed && (code == 67 || code == 86))) {
return true;
} else if (code == 17) {
is_ctrl_pressed = true;
} else {
return false;
}
});
$('#number_input').on('keyup', function(e) {
if (e.which == 17) {
is_ctrl_pressed = false;
}
});
Hope this will be helpful.
$('input[type="number"]').keypress(function(e){
//Numbers 47 to 57 are the key code of digit 0 to 9.
if (![48,49,50,51,52,53,54,55,56,57].includes(e.keyCode)){
e.preventDefault();
}
});
// Disable Right click
document.addEventListener('contextmenu', event => event.preventDefault());
// Disable key down
document.onkeydown = disableSelectCopy;
// Disable mouse down
document.onmousedown = dMDown;
// Disable click
document.onclick = dOClick;
function dMDown(e) { return false; }
function dOClick() { return true; }
function disableSelectCopy(e) {
// current pressed key
var pressedKey = String.fromCharCode(e.keyCode).toLowerCase();
if ((e.ctrlKey && (pressedKey == "c" || pressedKey == "x" || pressedKey == "v" || pressedKey == "a" || pressedKey == "u")) || e.keyCode == 123) {
return false;
}
}

Disable Right Click is not working on <embed> tag in Firefox, IE, but in Chrome is working fine

<script type="text/javascript">
$(document).ready(function () {
$(document).bind("contextmenu", function (e) {
e.preventDefault();
alert("Right Click not Available !!!");
});
});
</script>
<asp:Panel ID="pnlYoutube" runat="server" Style="text-align: center; height: 400px;
width: 700px; margin-left: 100px;">
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</asp:Panel>
In this Literal1 control we are passing embed player like:
<embed src='https://www.youtube.com/v/yh2SrzCkNQA?modestbranding=1&rel=0&showinfo=0&autoplay=1' type='application/x-shockwave-flash' id='youTubePlayer955'allowscriptaccess='never' enableJavascript ='false' allowfullscreen='false' width='700' height='400' ></embed>.
On chrome, disable right click is working fine, but in IE, Firefox on whole page right click disable is working but inside player disable right click is not working.
Try this code it will work in IE too
jQuery(document).ready(function() {
function disableSelection(e) {
if (typeof e.onselectstart != "undefined") e.onselectstart = function() {
return false
};
else if (typeof e.style.MozUserSelect != "undefined") e.style.MozUserSelect = "none";
else e.onmousedown = function() {
return false
};
e.style.cursor = "default"
}
window.onload = function() {
disableSelection(document.body)
};
window.addEventListener("keydown", function(e) {
if (e.ctrlKey && (e.which == 65 || e.which == 66 || e.which == 67 || e.which == 70 || e.which == 73 || e.which == 80 || e.which == 83 || e.which == 85 || e.which == 86)) {
e.preventDefault()
}
});
document.keypress = function(e) {
if (e.ctrlKey && (e.which == 65 || e.which == 66 || e.which == 70 || e.which == 67 || e.which == 73 || e.which == 80 || e.which == 83 || e.which == 85 || e.which == 86)) {}
return false
};
document.onkeydown = function(e) {
e = e || window.event;
if (e.keyCode == 123 || e.keyCode == 18) {
return false
}
};
document.oncontextmenu = function(e) {
var t = e || window.event;
var n = t.target || t.srcElement;
if (n.nodeName != "A") return false
};
document.ondragstart = function() {
return false
};
});
Try something like this:
document.onmousedown=disableRightclick;
var message="Right Click not Avaibalbe !!!";
function disableRightclick(evt)
{
if(evt.button==2)
{
alert(message);
return false;
}
}
<h1>Try right clicking here </h1>
<embed src='https://www.youtube.com/v/yh2SrzCkNQA?modestbranding=1&rel=0&showinfo=0&autoplay=1' type='application/x-shockwave-flash' id='youTubePlayer955'allowscriptaccess='never' enableJavascript ='false' allowfullscreen='false' width='700' height='400' ></embed>.

javascript : key validation

im using javascript to validate keys in textbox. it is not working :(
function numeric(e) {
return ((e.keyCode == 8) ||
(e.keyCode == 9) ||
(e.keyCode > 47 && e.keyCode < 58) ||
(e.keyCode > 36 && e.keyCode < 41) ||
(e.keyCode == 46) ||
(e.keyCode > 95 && e.keyCode < 106) ||
e.keyCode == 190 ||
e.keyCode == 110);
}
help me...
function numeric(e) {
e = e || window.event;
keycode = e.keyCode || e.which;
if(keycode === 13){
alert("cheese");
}
}
I know that in I.E. you can set event.keyCode=0 to suppress the key appearing in the control. But I think you need to trap the onkeydown. Firefox might have an equivalent. This is good because it prevents the key actually "arriving" at the control.
Also keep in mind that you might need to handle combinations of Shift + key and alt + key.
a good debug technique for this sort of thing is to say windows.status = event.keyCode,
and you can see what the keycode is as you type it...
Just try out the following code. I have checked F5 keycode, you can check as you want
function disableKey(event)
{
if (!event) event = window.event;
if (!event) return;
var keyCode = event.keyCode ? event.keyCode : event.charCode;
if (keyCode == 116) {
showMsg("This functionality is disabled.");
window.status = "F5 key detected! Attempting to disabling default response.";
window.setTimeout("window.status='';", 2000);
// Standard DOM (Mozilla):
if (event.preventDefault) event.preventDefault();
//IE (exclude Opera with !event.preventDefault):
if (document.all && event && !event.preventDefault) {
event.cancelBubble = true;
event.returnValue = false;
event.keyCode = 0;
}
return false;
}
}
function setEventListenerForFrame(eventListener)
{
document.getElementById('your_textbox').onkeydown = eventListener;
//frames['frame'].document.onkeypress = eventListener;
}
<body onload="setEventListener(disableKey);">
Try this if you want a numbers only textbox:
function numbercheck(event) {
var unicode = event.charCode; var unicode1 = event.keyCode; if (navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Safari") != -1) {
if (unicode1 != 8) {
if ((unicode >= 48 && unicode <= 57) || unicode1 == 37 || unicode1 == 39 || unicode1 == 35 || unicode1 == 36 || unicode1 == 9 || unicode1 == 46)
{ return true; }
else
{ return false; }
}
}
if (navigator.userAgent.indexOf("MSIE") != -1 || navigator.userAgent.indexOf("Opera") == -1) {
if (unicode1 != 8) {
if (unicode1 >= 48 && unicode1 <= 57)
{ return true; }
else
{ return false; }
}
}
}
And in your textbox call it on the onkeypress event:
onkeypress="return numbercheck(event)"

JavaScript keydown error in IE

<script>
var personX = 18;
var personY = 13;
function processArrowKeys(E) {
if (E.keyCode == 37 || E.keyCode == 38 || E.keyCode == 39 || E.keyCode==40) {
E.preventDefault();
}
if (E.keyCode == 37) {
if (currentterrain[personX - 1][personY] == 0 || currentterrain[personX - 1][personY] == 1 || currentterrain[personX - 1][personY] == 3) {
personX--;
}
}
if (E.keyCode == 39) {
if (currentterrain[personX + 1][personY] == 0 || currentterrain[personX + 1][personY] == 1 || currentterrain[personX + 1][personY] == 3) {
personX++;
}
}
if (E.keyCode == 38) {
for (i = 0; i < 3; i++) {
if (currentterrain[personX][personY - 1] == 0 || currentterrain[personX][personY - 1] == 1 || currentterrain[personX][personY - 1] == 3) {
personY--;
}
}
}
}
</script>
<body onkeydown="processArrowKeys(event)">
The IE debugger says that it expects an object and brakes on "handleArrowKeys(event)".
This works in FF and Chrome
I don't know why this fails, but it does.
changing this line fixed it:
if(E.keyCode==37||E.keyCode==38||E.keyCode==39||E.keyCode==40){if(navigator.appName!="Microsoft Internet Explorer"){E.preventDefault();}}
IE must not work with preventDefault()
Try the following:
/* ... */
function processArrowKeys(E) {
if (!E) E = window.event;
/* ... */
This should work out:
onload = function() {
var body = document.body,
personX = 18,
personY = 13;
body.onkeydown = function( E ) {
E = E || window.event;
if (E.keyCode == 37 || E.keyCode == 38 || E.keyCode == 39 || E.keyCode==40) {
if ( E.preventDefault ) {
E.preventDefault();
} else {
E.returnValue = false;
}
}
if (E.keyCode == 37) {
if (currentterrain[personX - 1][personY] == 0 || currentterrain[personX - 1][personY] == 1 || currentterrain[personX - 1][personY] == 3) {
personX--;
}
}
if (E.keyCode == 39) {
if (currentterrain[personX + 1][personY] == 0 || currentterrain[personX + 1][personY] == 1 || currentterrain[personX + 1][personY] == 3) {
personX++;
}
}
if (E.keyCode == 38) {
for (i = 0; i < 3; i++) {
if (currentterrain[personX][personY - 1] == 0 || currentterrain[personX][personY - 1] == 1 || currentterrain[personX][personY - 1] == 3) {
personY--;
}
}
}
}
}
Change
<body onkeydown="handleArrowKeys(event)">
To
<body onkeydown="processArrowKeys(event)">
Not sure how it works in firefox and chrome since there is no handleArrowKeys function.

Categories

Resources