Reputation: 1662
I use the following function for integer validation on keydown event.now i need to allow comma and space bar within this .how to do this?
function intValidate(event) {
if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||(event.keyCode == 65 && event.ctrlKey === true) ||(event.keyCode >= 35 && event.keyCode <= 39))
{
return;
}
else
{
if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )&&event.keyCode < 188)
{
event.preventDefault();
}
}
}
Upvotes: 1
Views: 2400
Reputation: 6401
You can call this function:
var isValid = function(key) {
var allowedCharacters = ['0','1','2','3','4','5','6','7','8','9',' ', ',']
if (allowedCharacters.contains(key)) return true;
return false;
};
Here is a fiddle: http://jsfiddle.net/Js9wQ/
Upvotes: 0
Reputation: 129802
Check for the key codes 188 and 32, respectively. Whereas 188 is "comma", you may also want to check for 110 – "decimal point", for the character on your numpad (depending on keyboard layout, of course).
Upvotes: 3
Reputation: 17194
32
will be for Space.
188
will be for Comma(,).
So Add it in your if
condition:
event.keyCode == 32 || event.keyCode == 188
See the whole list: KeyCode List
Upvotes: 0
Reputation: 37576
Add the Keycodes for the new Chars you need to allow in your If statement. tr this Keycodes List
Upvotes: 1