So I have the following code that detects where the key that was pressed is a number, space or delete key. If not it stops the key from being entered into the textfield. It works perfectly in Chrome and IE. When I run it in FireFox I get the following error: returnValue is undefined in the following statement: e.event.returnValue = false;
Here is the code:
keydown:function( sender, e, eOpts )
{
if (!isNumberKey(e))
{
e.event.returnValue = false;
}
}
The Function that does the work:
function isNumberKey(e)
{
//Local Varaible Declaration
var returnValue = false;
if (e.keyCode >= 96 && e.keyCode <= 105)
{
returnValue = true;
}
else if (e.keyCode >= 48 && e.keyCode <= 57)
{
returnValue = true;
}
else if (e.keyCode == 8 || e.keyCode == 46)
{
returnValue = true;
}
return returnValue;
}
I looked in the debugger in the firefox and found that returnValue really is not there. What do I use instead? I am sure there must be a way to accomplish this in FireFox.
Thanks,
Josh
It took me like 4 hours but here is the solution. Hope this helps:
keydown:function( sender, e, eOpts )
{
if (!isNumberKey(e))
{
if (Ext.browser.is.Firefox)
{
e.event.preventDefault();
}
else
{
e.event.returnValue = false;
}
}
}
Related
I have a requirement where I have to prevent user from typing in shift+greater than in textbox.
I looked up in the ascii key code chart.I could see no ascii key for shift+greater than combination which renders ">" on the UI.
This is the code that i have tried so far.
$scope.isValidControlInputInteger = function (event) {
var keyCode = event.keyCode;
if (keyCode >= 48 && keyCode <= 57 && event.shiftKey) { // decimal numbers
return true;
} else if (keyCode >= 96 && keyCode <= 105) { // numerical pad
return true;
} else if (keyCode == 46 || keyCode == 8) { // delete and backspace
return true;
} else if (keyCode == 37 || keyCode == 39) { // arrow keys
return true;
}
else if (keyCode == 9) { // tab key
return true;
}
else {
return false;
}
};
A simple workaround that works better than checking for keyup is to just remove all instances of > upon changing the contents of the input field.
$("#field").on("keyup", function(e) {
$(this).val($(this).val().replace(/\>/g, ""))
});
Here's a fiddle.
I want to disable the ctrl key in the IE browser.I had tried some solution using javascript but nothing is working can someone please help me to find out the solution
document.onkeydown = function () {
if (event.keyCode == 17) alert('Ctrl Key is disabled');
};
document.onkeydown = function(e) {
if (e.altKey && (e.keyCode === 36)) {//Alt+home blocked.
return false;
}
if (e.altKey && (e.keyCode === 70)) {//Alt+f blocked.
return false;
}
};
function hookKeyboardEvents(e) {
// get key code
var key_code = (window.event) ? event.keyCode : e.which;
// case :if it is IE event
if (window.event)
{
if (!event.shiftKey && !event.ctrlKey) {
window.event.returnValue = null;
event.keyCode = 0;
}
}
// case: if it is firefox event
else
e.preventDefault();
}
window.document.onkeydown = hookKeyboardEvents;
function Disable_Control_C() {
var keystroke = String.fromCharCode(event.keyCode).toLowerCase();
if (event.ctrlKey && (keystroke == 'c' || keystroke == 'v' || keystroke == 'p' || keystroke == 's' || keystroke == 'u')) {
alert("this function is disabled");
event.returnValue = false; // disable Ctrl+C
}
}
<body onkeydown="javascript:Disable_Control_C()">
this is what i do it to run in the IE...
I have a problem. Basically, what happens in my case is that the numbers in my textbox are autoformatted as I type. I don't want this to happen. What I want is that the numbers should be autoformatted only when the user clicks outside the textbox.
In my input tag I have :
onkeyup="format(event, this);"
My javascript function is :
function format(e, obj) {
if (e.keyCode == 36) {
press1(obj);
}
if (e.keyCode == 13) {
return false;
}
if ((e.keyCode <= 34) || (e.keyCode >= 46 && e.keyCode < 58) || (e.keyCode >= 96 && e.keyCode <= 105)) { // //alert(e.keyCode);
obj.value = CommaFormatted(obj.value);
} else {
if (e && e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
} else {
e.cancelBubble = true;
e.returnValue = false;
}
return false;
}
}
where the press1 function is:
function press1(textControlID) {
var text = textControlID;
if (text.getAttribute("maxlength") == text.value.length) {
var FieldRange = text.createTextRange();
FieldRange.moveStart('character', text.value.length);
FieldRange.collapse();
FieldRange.select();
return true;
}
if (text != null && text.value.length > 0) {
if (text.createTextRange) {
var FieldRange = text.createTextRange();
FieldRange.moveStart('character', text.value.length);
FieldRange.collapse();
FieldRange.select();
} else if (text.setSelectionRange) {
var textLength = text.value.length;
text.setSelectionRange(textLength, textLength);
}
}
}
I really hope this could be solved. Please!
You could change onkeyup to onblur, which is the event that gets fired when the control loses focus - clicking out of it.
The onkeyup event fires with every keypress.
In my chat application there are some text fields which gets the user login details.
when filling the user details,If user suddenly pressed the ESC key,the data will be lost.
I need to disable the function of ESC key ? which event I need to use ? how can I do that.
my Java Script code is ,
function esc(e){
e = e || window.event || {};
var charCode = e.charCode || e.keyCode || e.which;
if(charCode == 27){
return false;
}
}
Searched a lot in Stack overflow and google.Nothing worked.Please any one help me to do that . Thanks..
You can bind an eventlistener to your input field to catch the Event when Esc is pressed and supress it.
document.querySelector("input").addEventListener("keydown",function(e){
var charCode = e.charCode || e.keyCode || e.which;
if (charCode == 27){
alert("Escape is not allowed!");
return false;
}
});
Example
I got the solution to control the " F5 , Esc , BackSpace(BS) " keys with the following code.
My Java Script code will be ,
document.attachEvent("onkeydown", win_onkeydown_handler);
function win_onkeydown_handler() {
switch (event.keyCode) {
case 116 : // 'F5'
event.returnValue = false;
event.keyCode = 0;
break;
case 27: // 'Esc'
event.returnValue = false;
event.keyCode = 0;
break;
case 08: // 'BackSpace'
if (event.srcElement.tagName == "INPUT"
|| event.srcElement.tagName == "TEXTAREA") {
} else {
event.returnValue = false;
event.keyCode = 0;
}
break;
}
}
Thanks who are all supported me to do this and for your suggestions.
I have used this for a login popup code:
jQuery(document).keyup(function(e){
if(e.keyCode==27 && popupStatus==1){
// alert('not allowed !!!');
// or any other code
return false;
}
});
I have done something similar using jquery to limit entry to numbers
$(inputBox).keydown(function(event) {
// Allow only backspace and delete
var allowed_keys = [
46, // delete
8, // backspace
];
if ($.inArray(event.keyCode, allowed_keys) != -1) {
// let it happen, don't do anything
}
else {
// Ensure that it is a number and stop the keypress
if (event.keyCode < 48 || event.keyCode > 57 ) {
event.preventDefault();
}
}
});
Have the following piece of javascript over on http://jsfiddle.net/mhenrixon/zPLgS/16/ and its working fine in regular browsers but I can't make it work in IE Mobile for Windows CE 6.0 where it needs to work. Anyone knows how to fix that?
function tab(field, event) {
if (event.which == 13 /* IE9/Firefox/Chrome/Opera/Safari */ || event.keyCode == 13 /* IE8 and earlier */ ) {
for (i = 0; i < field.form.elements.length; i++) {
if (field.form.elements[i].tabIndex == field.tabIndex + 1) {
field.form.elements[i].focus();
if (field.form.elements[i].type == "text") {
field.form.elements[i].select();
break;
}
}
}
return false;
}
return true;
}
What if you were to just change the event.which or event.keyCode from ENTER to TAB, and let the default behavior go through? Something like:
function tab(event) {
if (event.which && event.which == 13)
event.which = 9;
else if (event.keyCode && event.keyCode == 13)
event.keyCode = 9;
}
Not sure if they key codes are different for Windows Mobile, but to figure that out all you need to do is alert(event.which); or alert(event.keyCode);.
I cannot test any of these links because I dont have any mobile device with Windows Mobile but maybe there is something helpful for you:
Another post with same issue
A workaround
KeyCodes MSDN
Another helpful link
Thanks guys the problem was not getting the key and it was not changing propagation or anything like that it was actually quite easy.
Full solution
function tab(field, event) {
if (event.which == 13 /* IE9/Firefox/Chrome/Opera/Safari */ || event.keyCode == 13 /* IE8 and earlier */) {
nextElement(field);
return false;
}
return true;
}
function nextElement(field) {
for (var i = 0; i < field.form.elements.length; i++) {
if (field.form.elements[i].tabIndex == field.tabIndex + 1) {
field.form.elements[i].focus();
if (field.form.elements[i].type == "text") {
field.form.elements[i].select();
break;
}
}
}
}