Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #18121
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | "Tony" <tony.johansson@inport.com> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: Want to construct a function instead of the code for onkeyup |
| Date | Thu, 17 Jan 2013 09:10:45 +0100 |
| Organization | A noiseless patient Spider |
| Lines | 1 |
| Message-ID | <kd8bm9$aat$1@dont-email.me> (permalink) |
| References | <kd77dq$tph$1@dont-email.me> <37748651.8yA5jalnsy@PointedEars.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; format=flowed; charset="UTF-8"; reply-type=original |
| Content-Transfer-Encoding | 8bit |
| Injection-Date | Thu, 17 Jan 2013 08:10:49 +0000 (UTC) |
| Injection-Info | mx04.eternal-september.org; posting-host="04290fff4557273ca84c3ad616751e0b"; logging-data="10589"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Wl7hc1YkRcaRMicrM9n7zbXwVecTzmgg=" |
| X-MimeOLE | Produced By Microsoft MimeOLE V15.4.3555.308 |
| In-Reply-To | <37748651.8yA5jalnsy@PointedEars.de> |
| X-Newsreader | Microsoft Windows Live Mail 15.4.3555.308 |
| Importance | Normal |
| Cancel-Lock | sha1:1R6T4D2N0GHut0hmfs3YYk77HMw= |
| X-Priority | 3 |
| X-MSMail-Priority | Normal |
| Xref | csiph.com comp.lang.javascript:18121 |
Show key headers only | View raw
I have now replaced the code for onkeyup with a function. See below.
The function name is RemoveInvalidCharacters and it is listed below. When I
call this function I get the following error
which is transladed from swedish
Runtime error in javascript: It's not possible to get properties replace for
a reference that is undefined or null
So why do I get this error if I keep the code in onkeyup it works fine ?
function RemoveInvalidCharacters()
{
if (/[^0-9\.,]/g.test(this.value))
{
// Filter non-digits from input value.
this.value = this.value.replace(/[^0-9\.,]/g, '');
}
}
<input type="text" name="depthSteern" id="txtDepthSteern" size="21"
onkeyup="if (/[^0-9\.,]/g.test(this.value)) this.value =
this.value.replace(/[^0-9\.,]/g,'')" />
<input type="text" name="depthBow" id="txtDepthBow" size="20"
onkeyup="RemoveInvalidCharacters()" />
"Thomas 'PointedEars' Lahn" wrote in message
news:37748651.8yA5jalnsy@PointedEars.de...
Tony Johansson wrote:
> How should I write a function if I want to move the code
> that is executed for onkeyup ?
>
> <input type="text" name="depthBow" id="txtDepthBow" size="20"
> onkeyup="if (/[^0-9\.,]/g.test(this.value))
> this.value = this.value.replace(/[^0-9\.,]/g,'')" />
You should pass the “this” value, here a DOM object reference, as argument
to the function:
<https://developer.mozilla.org/en-
US/docs/JavaScript/Reference/Functions_and_function_scope>
<http://jibbering.com/faq/>
<http://PointedEars.de/faq>
And do not use the RegExp flag “g(lobal)” with RegExp.prototype.test(), as
it is a wrapper around RegExp.prototype.exec() [ES 5.1, § 15.10.6.3]:
var rx = /x/g;
/* true */
rx.test("x")
/* false */
rx.test("x")
/* true */
rx.test("x")
That another /RegularExpressionLiteral/ creates a new RegExp object is a
change from ECMAScript Ed. 3 to 5 [Annex E]; do not rely on it unless you
know what you are doing.
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
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