Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #18125
| Newsgroups | comp.lang.javascript |
|---|---|
| Subject | Re: Want to construct a function instead of the code for onkeyup |
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
| References | <kd77dq$tph$1@dont-email.me> <37748651.8yA5jalnsy@PointedEars.de> <kd8bm9$aat$1@dont-email.me> |
| Date | 2013-01-17 15:40 +0100 |
| Message-ID | <XnsA14B9F7BE43B5eejj99@194.109.133.133> (permalink) |
Tony wrote on 17 jan 2013 in comp.lang.javascript:
> function RemoveInvalidCharacters()
> {
> if (/[^0-9\.,]/g.test(this.value))
> {
> // Filter non-digits from input value.
> this.value = this.value.replace(/[^0-9\.,]/g, '');
> }
> }
= The "this" does not automagically follow you into the function. You will
have to send the object as a function parameter.
= You do not have to test first to remove characters,
as only characters that are present can be removed.
Stands to reason, doesn't it?
= [\d] is the same as [0-9]
= [.] does not need to be escaped.
Try this [better don't, read on]:
=============================
<input onkeyup = 'removeInvalidCharacters(this)'
function removeInvalidCharacters(x) {
x.value = x.value.replace(/[^\d.,]/g, '');
};
=============================
Even then onkeyup will not work as expected on all browsers, I believe I
remember, as some browser input value will only update when the cursor
leaves the input. Not tested however. And your user does not like to have
dead keys on his/her keyboard, (s)he will hate you for that. And perhaps
the character cursor will jump to the start when you replace the input
value, also not something to make your user happy.
You are making mistakes many of us made in the last century. No matter,
try to learn from it.
I would suggest testing or correcting the values onblur or even better on
form submit.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Want to construct a function instead of the code for onkeyup "Tony Johansson" <johansson.andersson@telia.com> - 2013-01-16 22:51 +0100
Re: Want to construct a function instead of the code for onkeyup Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-16 23:29 +0100
Re: Want to construct a function instead of the code for onkeyup "Tony" <tony.johansson@inport.com> - 2013-01-17 09:10 +0100
Re: Want to construct a function instead of the code for onkeyup "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-01-17 15:40 +0100
Re: Want to construct a function instead of the code for onkeyup "Tony" <tony.johansson@inport.com> - 2013-01-17 09:23 +0100
csiph-web