Here I have a text box and I want to allow spanish vowels such as ¨áéióú¨ and all spanish letters. I have return the javascript function as follows.
if ((key > 64 && key < 91)|| (key > 159 && key < 166) || (key > 96 && key < 123) || (key == 165) ||(key == 32)
|| (key == 164) || (key == 130) || (key == 181) || (key == 144) || (key == 214) ||
(key == 224) ||(key == 233) || (key == 8) || (key == 241) || (key == 209)) {
}else{
event.preventDefault();
}
I have allowed the ñ and Ñ Beacuse Those letters are present in Keyboard. So I debugged the code And I found the keycodes and I allowed them. For the other letters I canot able to find the keycodes. Can any one help me to find the keycodes for the spanish letters
.
Just restrict only letters that you don't want to allow present in your keyboard. If you enter other than that letters it will be allowed
Related
I have a input box where I am allowing users to use alphabets, arrow, home, del etc keys. I want to prevent users to enter "?" question mark.
Here is my code:
$(document).on('keydown', "#element", function(e) {
var key = e.which || e.keyCode;
if (!e.altKey && !e.ctrlKey &&
// Alphabets
key >= 65 && key <= 120 ||
// Backspace and Tab and Enter
key == 8 || key == 9 || key == 13 ||
// Home and End
key == 35 || key == 36 ||
// left and right arrows
key == 37 || key == 39 || key == 50 || key == 191 ||
// Del and Ins
key == 46 || key == 45)
return true;
return false;
});
I know that "/" key code is 191. I am unable to prevent the question mark input. How can I do that? Any help please.
Since you know the key code is 191, you just need to check if the shift key was being pressed at the same time.
if (e.shiftKey && key === 191)
So, in your code, you can just add (e.shiftKey && key === 191).
I am using JavaScript keycodes, they are not working in Firefox but working in Chrome and IE. I have debugged the code in front end for Firefox I am getting keycode as 0.
This is my code:
$scope.Validate = function(event,indexVal){
if ((event.keyCode > 64 && event.keyCode < 91)|| (event.keyCode > 159 && event.keyCode < 166) || (event.keyCode > 96 && event.keyCode < 123) || (event.keyCode == 165) ||(event.keyCode == 32)
|| (event.keyCode == 164) || (event.keyCode == 130) || (event.keyCode == 181) || (event.keyCode == 144) || (event.keyCode == 214) ||
(event.keyCode == 224) ||(event.keyCode == 233)) {
}else{
event.preventDefault();
}
}
Can you please suggest a way to achieve this functionality in Firefox too.
In Firefox, event.keyCode does not always work, depending on the binding event. You'll have to use event.which. Refer to this post for more info.
$scope.Validate = function(event,indexVal) {
var key = event.keyCode || event.which;
if ((key > 64 && key < 91) ||
(key > 159 && key < 166) ||
(key > 96 && key < 123) ||
(key == 165) ||
(key == 32) ||
(key == 37) ||
(key == 39) ||
(key == 164) ||
(key == 130) ||
(key == 181) ||
(key == 144) ||
(key == 214) ||
(key == 224) ||
(key == 233)
) {
// Do something.
} else {
event.preventDefault();
}
}
It's working but slightly different
$scope.Validate = function (event, indexVal) {
var code = event.which || event.keyCode;
if (!(
(code > 64 && code < 91)
|| (code > 159 && code < 166)
|| (code > 96 && code < 123)
|| ~[165, 32, 164, 130, 181, 144, 214, 224, 233].indexOf(code)
)) {
event.preventDefault();
}
}
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
Deprecated KeyboardEvent.keyCode
This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Avoid using it and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
I have tried to append the code from another post which works perfectly on fiddle on this link: http://jsfiddle.net/WfpEu/51/
The code replaces comma "," as soon as the user types it and turns it to dot "."
$.fn.ForceNumericOnly = function() {
return this.each(function() {
$(this).keydown(function(e) {
if(e.keyCode==188 || e.keyCode==110 || e.keyCode==108){
e.preventDefault();
$(this).val($(this).val() + '.');
}
var key = e.charCode || e.keyCode || 0;
return (key == 8 || key == 9 || key == 46 || key == 110 || key == 188 || key == 190 || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105));
});
});
};
$(".item").ForceNumericOnly();
I have tried to append it to my code here http://jsfiddle.net/p2Hbm/104/ and it doesn't work as it should and i don't know what is wrong. I can't write letters in input fields which is good but also i can't write comma "," it doesn't show up at all.
You need to add the itemclass to your input fields.
Working fiddle: http://jsfiddle.net/p2Hbm/106/
i have a requirement: a text box can have a max of 50 characters, to achieve this I gave preventDefault.
if (!(event.ctrlKey && (event.which == 65 || event.which == 86 || event.which == 67 || event.which == 88))
&& (!(event.shiftKey && (event.which == 35 || event.which == 36)))
&& (key != 8 && key != 37 && key != 39 && key != 46 && key != 36 && key != 35 && key != 33 && key != 34)) {
event.preventDefault();
}
But now the requirement is : when we press ctrl+a and then abc it should erace everything from the text and print abc.
to achieve this i wrote this code ->
var focusItem = $('<input type="text">');
function getSelText() {
var txt = '';
//IE
var focusItem = $('<input type="text">');
if (document.selection != undefined) {
focusItem.focus();
var sel = document.selection.createRange();
var rangeParent = sel.parentElement();
txt = sel.text;
if (!(event.ctrlKey && (event.which == 65 || event.which == 86 || event.which == 67 || event.which == 88))
&& (!(event.shiftKey && (event.which == 35 || event.which == 36)))
&& (key != 8 && key != 37 && key != 39 && key != 46 && key != 36 && key != 35 && key != 33 && key != 34)) {
//event.preventDefault();
if (txt.length > 0 && ((key >= 65 && key <= 90) || (key >= 48 && key <= 57) || (key >= 97 && key <= 122))) {
sel.text = String.fromCharCode(key).toLowerCase();
}
txt = sel.text;
}
}
return txt;
}
now things are working but.. when I press ctrl+a abc then its getting printed as bca. Whats happening is : When i press a after ctrl+a then a is getting printed on the textbox but the focus is getting set before the text, that is a.
How to deal with this.
I am using windows 7, IE9, visual studio 2010.
Thanks.
The easiest way to set the focus to the end of a textbox is to just set the text of the textbox to itself:
focusItem.val(focusItem.val());
I found the following jQuery code on the internet, but I soon found that it had a deficiency in that it dit not accept a decimal point (ascii code 46) - even though the code appears to allow it.
Currently, I cannot enter prices like 1.23, since the the period is ignored and I get 123 instead.
Can anyone spot whty this is not working?
// Numeric only control handler
$.fn.ForceNumericOnly =
function()
{
return this.each(function()
{
$(this).keydown(function(e)
{
var key = e.charCode || e.keyCode || 0;
// allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
return (
key == 8 ||
key == 9 ||
key == 46 ||
(key >= 37 && key <= 40) ||
(key >= 48 && key <= 57) ||
(key >= 96 && key <= 105));
})
})
};
I am using the plugin like this:
$(function(){
$('#price_field').ForceNumericOnly();
});
The other users have pretty much answered your question already, but I wanted to provide you with this link.
http://unixpapa.com/js/key.html
I found it quite useful when dealing with keyboard events and making them cross-browser compatible.
I hope this helps.
Just add the . (code 190 and 110) to the checks:
// Numeric only control handler
$.fn.ForceNumericOnly =
function()
{
return this.each(function()
{
$(this).keydown(function(e)
{
var key = e.charCode || e.keyCode || 0;
// allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
return (
key == 8 ||
key == 9 ||
key == 46 ||
key == 190 || // normal .
key == 110 || // keypad .
(key >= 37 && key <= 40) ||
(key >= 48 && key <= 57) ||
(key >= 96 && key <= 105));
})
})
};
You should add the key code 190 to accept "."
// Numeric only control handler
$.fn.ForceNumericOnly = function() {
return this.each(function()
{
$(this).keydown(function(e)
{
var key = e.charCode || e.keyCode || 0;
// allow dot, backspace, tab, delete, arrows, numbers and keypad numbers ONLY
return (
key == 190 ||
key == 8 ||
key == 9 ||
key == 46 ||
(key >= 37 && key <= 40) ||
(key >= 48 && key <= 57) ||
(key >= 96 && key <= 105));
})
})
};
You need to include 190 and 110 for both decimal points.
// Numeric only control handler
$.fn.ForceNumericOnly =
function()
{
return this.each(function()
{
$(this).keydown(function(e)
{
var key = e.charCode || e.keyCode || 0;
// allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
return (
key == 190 || //add this line. 190 is the keycode for a period
key == 110 || //and this line. 110 is the keycode for a decimal
key == 8 ||
key == 9 ||
key == 46 ||
(key >= 37 && key <= 40) ||
(key >= 48 && key <= 57) ||
(key >= 96 && key <= 105));
})
})
};