I have created a function for input mask with jQuery:
$.fn.mask = function(regex) {
this.on("keydown keyup", function(e) {
if (regex.test(String.fromCharCode(e.which))) {
return false;
}
});
}
It denies any input based on the regex you pass. Examples:
$("#textfield").mask(/\s/); // denies white spaces
$("#textfield").mask(/\D/); // denies anything but numbers
Those examples above works, but I'm trying to use a regex to accept numbers with decimal separator, like this:
$("#textfield").mask(/[^\d.,]/); // Anything except digits, dot and comma
That doesn't work. But if I log String.fromCharCode(e.which)
on console when I press .
or ,
it shows me these(respective) chars: ¾
and ¼
.
The question is why String.fromCharCode(e.which)
stands for those chars instead of the pressed ones?