Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
59
rated 0 times [  62] [ 3]  / answers: 1 / hits: 42961  / 14 Years ago, tue, may 18, 2010, 12:00:00

Im writing some logic in JavaScript using jquery, where i must check the input content against a REGEX pattern ex:



^[a-zA-Z0-9_]*$  //Alpha-numeric and _


The logic is almost done, i just have a little problem filtering the function key DEL,
my logic goes like this:



var FunctionsKey = new Array(8, 9, 13, 16, 35, 36, 37, 39, 46);

function keypressValidation(key) {
if (config.regexExp != null) {
if ($.inArray(key, FunctionsKey) != -1) {
return true;
}
else {
var keyChar = String.fromCharCode(key);
return RegexCheck(keyChar);
}
}
return true;
}


If the KeyCode is one of those in the array, i let it pass, if not i get the char and compare it against the REGEX.
The problem is: in some Browsers the DEL and '.' (period sign) have the same key Code 46.



So is there a better logic to filter the function keys or must i write a condition for that case, maybe removing the 46 from the array and try to convert it to char and if is (.) let it go to the Regex function if not let it pass?
The other question will be are there more shared Key Codes in some browsers?



EDIT: My suggested solution wont work because it doesn't matter which key the user pressed (DEL or period) i always get (.) as CHAR at least on OPERA and FF =(.


More From » jquery

 Answers
23

110 is the decimal key code, 46 is the DEL key.



For some fun: put this in to see what you hit! EDIT: added a focused event



   /* handle special key press */
$(document).ready(function() {
function checkAKey(e) {
var shouldBubble = true;
switch (e.keyCode) {
// user pressed the Tab
case 9:
{
alert(Tab hit, no bubble);
shouldBubble = false;
break;
};
// user pressed the Enter
case 13:
{
alert(Enter);
break;
};
// user pressed the ESC
case 27:
{
alert(Escape);
break;
};
};
/* this propogates the jQuery event if true */
return shouldBubble;
};

$(*).keydown(function(e) {
return checkAKey(e);

});
});


OR



$(document).ready(function() {
/* handle special key press */
function checkFieldKey(e, me) {
var shouldBubble = true;
switch (e.keyCode) {
// user pressed the Enter
case 13:
{
$(me).blur();
$(#somewhereElse).focus();
shouldBubble = false;
break;
};
};
/* this propogates the jQuery event if true */
return shouldBubble;
};
/* user pressed special keys while in Selector */
$(#myField).keydown(function(e) {
return checkFieldKey(e, $(this));
});
});

[#96747] Monday, May 17, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
donaldcristianl

Total Points: 114
Total Questions: 95
Total Answers: 110

Location: Bonaire
Member since Sat, May 27, 2023
1 Year ago
;