I'm working on some javascript in a page with charset=utf8. We are hoping to limit certain input fields to numbers only. Someone showed me this code and it struck me that it refers to specific character codes which seems a little strange.
var restrictDollarToNum = function(e, source) {
var restrictionType = /[1234567890.]/g;
if (!e) e = window.event;
if (e.keyCode) {
code = e.keyCode;
} else if (e.which) {
code = e.which;
}
if (code >= 96 && code <= 105) {
code -= 48;
}
var character = String.fromCharCode(code);
if (!(character.match(restrictionType) || code == 8 || code == 13 || code == 46)) {
source.value = source.value.replace(character, '');
source.value = source.value.replace(character.toLowerCase(), '');
} else {
// maybe recaclulate something
}
};
I was wondering a couple of things:
1) these keycodes -- do they refer to members of my charset or to key ordinals on my qwerty keyboard or something like that?
2) While this sort of thing may work on a US or UK english keyboard, might the codes be different on keyboards for other languages?
3) Is there perhaps a better way to do this?