Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #18116 > unrolled thread
| Started by | "Tony Johansson" <johansson.andersson@telia.com> |
|---|---|
| First post | 2013-01-16 22:51 +0100 |
| Last post | 2013-01-17 09:23 +0100 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.javascript
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
| From | "Tony Johansson" <johansson.andersson@telia.com> |
|---|---|
| Date | 2013-01-16 22:51 +0100 |
| Subject | Want to construct a function instead of the code for onkeyup |
| Message-ID | <kd77dq$tph$1@dont-email.me> |
Hello! 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,'')" /> //Tony
[toc] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-01-16 23:29 +0100 |
| Message-ID | <37748651.8yA5jalnsy@PointedEars.de> |
| In reply to | #18116 |
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.
[toc] | [prev] | [next] | [standalone]
| From | "Tony" <tony.johansson@inport.com> |
|---|---|
| Date | 2013-01-17 09:10 +0100 |
| Message-ID | <kd8bm9$aat$1@dont-email.me> |
| In reply to | #18117 |
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.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2013-01-17 15:40 +0100 |
| Message-ID | <XnsA14B9F7BE43B5eejj99@194.109.133.133> |
| In reply to | #18121 |
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)
[toc] | [prev] | [next] | [standalone]
| From | "Tony" <tony.johansson@inport.com> |
|---|---|
| Date | 2013-01-17 09:23 +0100 |
| Message-ID | <kd8cet$eki$1@dont-email.me> |
| In reply to | #18117 |
It works now!
//Tony
"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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web