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;
}
}
}
}
Related
I have a web application that has to do with key pressing. It is functioning well across all browsers except for Chrome in mobile.
Here is the javascript code:
function O000OOO(e) {
var O0000O0;
if (window.event) {
if (window.event.type == "keypress") {
O00OO00 = -1
}
if (window.event.type == "keypress") {
O00OO00 = window.event.keyCode
}
if (parseInt(O00OO00) > 0) {
O0000O0 = O00OO00
} else {
O0000O0 = window.event.keyCode
}
} else {
if (e.type == "keypress") {
O00O0OO = e.which;
O00OO00 = -1
}
if (e.type == "keypress") {
O00OO00 = e.which
}
if (parseInt(O00OO00) > 0) {
O0000O0 = O00OO00
} else {
if ((parseInt(O00O0OO) > 0) && (e.which < 1)) {
O0000O0 = O00O0OO
} else {
O0000O0 = e.which
}
}
}
return (parseInt(O0000O0))
}
and 000000 is a string.
Does anyone have a solution for this problem?
I think you should be using the beforeinput event rather then keypress as it looks like keypress is depreciated so may not be supported. Have a look at this from w3:
https://www.w3.org/TR/uievents/#determine-keypress-keyCode
They specify partway through that:
Authors SHOULD use the beforeinput event instead of the keypress
event
So that is pretty clear.
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;
}
}
}
This validation return true when the input text is 0 to 10 (0,1,2,3,4,5,6,7,8,9,10).
The backspace do not work in Firefox ,in Google Chrome ,Internet Explorer work well.I tried to enable the charcode of the backspace but the problem persist.
How can I make it works in firefox ?
I have this:
<script>
function compruebacampo(evt, campotexto)
{
var charCodeOfZero = 48;
var numberJustEntered = evt.charCode - charCodeOfZero;
var fullString = campotexto.value + "" + numberJustEntered;
var matchesOne = false;
for (var i = 0; i <= 10; i++) {
if (fullString == ("" + i))
matchesOne = true;
}
if (!matchesOne)
return false;
}
</script>
<input type="textbox" onkeypress="return compruebacampo(event,this)" >
Any help would be very appreciated.
I was going to write a function that used browser-specific cases (via navigator.userAgent), but I figured that was too clunky and complicated. I think the easiest thing to do is to bind an event handler to that specific input and have it listen to the keyCodes (keys in FireFox). This method also avoids inline JavaScript, which is definitely a plus.
event.keyCode < 58 && event.keyCode > 47
For non-FireFox browsers, this captures all the digits (0-9).
event.key > -1 && event.key < 10
In FireFox, this will capture all the digits (0-9), since the above will fail.
event.keyCode == 8 || event.keyCode == 0
Captures backspaces and allows them to go through.
HTML
<input type="text" id="numberInput">
JavaScript
document.getElementById("numberInput").onkeypress = function (event) {
if (event.keyCode < 58 && event.keyCode > 47 || event.key > -1 && event.key < 10) {
/* nothing */
} else if (event.keyCode == 8 || event.keyCode == 0) {
/* nothing */
} else {
event.preventDefault();
}
}
I tested in Safari, FireFox, and Chrome. It worked in all three for me. I'm sure you could rewrite those booleans to just one if block.
fiddle
Try this alternative which will allow only numbers to be input on the field: http://jsfiddle.net/6faHh/
function compruebacampo(e, campotexto) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
return false;
}
}
$('input').keypress(function (e) {
if ($(this).attr('id') == "txtBoxId") {
regex = new RegExp("^[0-9\b]+$");
var charString = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(charString)) {
return true;
}
else {
e.preventDefault();
return false;
}
}
}
I have the following code to allow only numbers to be entered (other logic is removed for brevity).
$("input").keydown(function (event) {
var key = event.keyCode;
if ((key < 48 || key > 57) && (key < 96 || key > 105) || event.shiftKey) {
event.preventDefault();
}
});
This code works fine in English keyboards but on French keyboards the shift key is used so the logic fails there. If i remove the shift the logic fails in english keyboards.
Is there a way to detect a number is being pressed in the keydown event that will work on any type of keyboard?
Thanks
Use a custom function to check if the value of the keydown is numeric. From this previous answer (Validate decimal numbers in JavaScript - IsNumeric()):
function isNumber(n)
{
return !isNaN(parseFloat(n)) && isFinite(n);
}
And your handler UPDATED:
$("input").keydown(function (event) {
var code = event.keyCode;
//Allows left and right arrows, backspace, and delete
if(code == 37 || code == 39 || code == 8 || code == 46)
return;
var character = String.fromCharCode(code);
if(event.shiftKey || !isNumber(character)){
event.preventDefault();
}
});
I have found that event.key works better than event.keyCode. The event handler needs to be onkeydown for it to work properly. The check for whether it's a number needs to come first. Here's my code. Tested to work on IE11, Edge, Chrome, and Firefox.
$("input").keydown(function (event) {
var code = event.keyCode;
var key = event.key;
if (!isNaN(Number(key)))
return;
// allow backspace, delete, left & right arrows, home, end keys
if (code == 8 || code == 46 || code == 37 || code == 39 || code == 36 || code == 35) {
return;
} else {
evt.preventDefault();
}
});
#HostListener('keydown', ['$event']) onKeyDown(event) {
if ((e.keyCode < 48 || e.keyCode > 57) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
} else if (!this.isNumber(e.key)) {//For french keyboard
e.preventDefault();
}
}
}
isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
I had a similar problem because of 2 different keyboards. And I solve that by checking if is not a number in the key value instead of the keyCode value.
Would this work?
$("input").bind("propertychange input textInput", function() {
this.value = this.value.replace(/[^\d.]/g, "");
});
Of course, this trims the value after the input event, so I'm not sure if that's what you want
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();
}
}
});