field value AFTER numeric validation

F

fixertool

I have a textbox field with numeric validation using a function with
onkeypress event

<input type="text" name="xxx" id="xxx" size="8" maxlength="7"
onkeypress="IsNumeric(this);" />

The function is very simple and known (and it works fine):

function IsNumeric(e)
{
if (event.keyCode < 48 || event.keyCode > 57)
{
alert("Only Numbers, please.");
return false;
}
}

Let's say I try to input 12q. Well, when I press 'q' key, the alert
obviously jumps. Then I click alert's 'OK' button, obviously too.
The focus goes to the field again, and the field value now is 12q.
But I want the field value turns 12. That is, without the last non-
numeric key value pressed. Am I clear enough?

Thanks in advance
 
S

SAM

fixertool a écrit :
I have a textbox field with numeric validation using a function with
onkeypress event

<input type="text" name="xxx" id="xxx" size="8" maxlength="7"
onkeypress="IsNumeric(this);" />

onkeydown="return IsNumeric(event);"
The function is very simple and known (and it works fine):

function IsNumeric(e)
{
if (event.keyCode < 48 || event.keyCode > 57)

on my Mac :
- numeric-keyboard key codes are from 96 to 105 ...
- I need the shift key (16) to tape a number from alpha-keyboard
so :
 
E

Evertjan.

fixertool wrote on 31 mei 2008 in comp.lang.javascript:
I have a textbox field with numeric validation using a function with
onkeypress event

<input type="text" name="xxx" id="xxx" size="8" maxlength="7"
onkeypress="IsNumeric(this);" />

onkeypress="return IsNumeric(event);"

Thre is no reason to use />, use >
The function is very simple and known (and it works fine):

function IsNumeric(e)

The e is not called in the function.
{
if (event.keyCode < 48 || event.keyCode > 57)
{
alert("Only Numbers, please.");
return false;
}
}


Try, IE7 & FF2 tested:

==================================
<input type="text" onkeypress="return IsNumeric(event);">

<script type='text/javascript'>
function IsNumeric(e) {
var c = e.keyCode? e.keyCode : e.charCode
if (c >= 48 && c <= 57)
return true;
alert('Only Numbers, please.');
return false;
};
</script>
==================================

This will not prevent pasting alpha strings!!!!!
 
S

SAM

Evertjan. a écrit :
Try, IE7 & FF2 tested:

Not working on my Mac :-(
(FF2, Safari2)

keys with keyCode 48 to 57 need the shiftkey in addition to get a number
from alpha keyboard
- works with Safari
- not with F2 who return a keyCode for the shift's key
(not in the allowed plage :-( )

keys from numeric keyboard aren't more allowed ... ! :-(

But I can tape : @ & $ €

And no more backspace ... tab ... enter ... :-(
<input type="text" onkeypress="return IsNumeric(event);">

<script type='text/javascript'>
function IsNumeric(e) {
var c = e.keyCode? e.keyCode : e.charCode
if (c >= 48 && c <= 57)
return true;
alert('Only Numbers, please.');
return false;
};
</script>

This will not prevent pasting alpha strings!!!!!

Only if you use menu (and not use keyboard shortcuts)
 
E

Evertjan.

SAM wrote on 31 mei 2008 in comp.lang.javascript:
Evertjan. a écrit :

Not working on my Mac :-(
(FF2, Safari2)

keys with keyCode 48 to 57 need the shiftkey in addition to get a number
from alpha keyboard
- works with Safari
- not with F2 who return a keyCode for the shift's key
(not in the allowed plage :-( )

keys from numeric keyboard aren't more allowed ... ! :-(

But I can tape : @ & $ €

And no more backspace ... tab ... enter ... :-(


Only if you use menu (and not use keyboard shortcuts)

It is a bad idea anyway, as it interferes with typing habits.

Better test the string onsubmit.
 
T

Thomas 'PointedEars' Lahn

fixertool said:
I have a textbox field with numeric validation using a function with
onkeypress event

<input type="text" name="xxx" id="xxx" size="8" maxlength="7"
onkeypress="IsNumeric(this);" />
^
MSHTML which appears to be your target platform does support XHTML. Serving
XHTML als text/html is possible but recommended against anyway because it is
relying on *broken* *error-correction*.

http://hixie.ch/advocacy/xhtml
The function is very simple and known (and it works fine):

It is supposed to work fine in Internet Explorer and other MSHTML-based UAs
only, because:
function IsNumeric(e) ^
{
if (event.keyCode < 48 || event.keyCode > 57)
^^^^^
This is quite pointless. You pass `this' which is a reference to the object
firing the event. You name the argument `e' maybe because you think that it
is a reference to the event object instead, but you don't use `e' anywhere.

Then you use the MSHTML-proprietary global `event' property and keyCode when
you should have used a more compatible event object reference and both
keyCode and charCode.

http://www.brain4.de/programmierecke/js/tastatur.php
{
alert("Only Numbers, please.");
return false;
}
}

Let's say I try to input 12q. Well, when I press 'q' key, the alert
obviously jumps. Then I click alert's 'OK' button, obviously too.
The focus goes to the field again, and the field value now is 12q.
But I want the field value turns 12. That is, without the last non-
numeric key value pressed. Am I clear enough?

Same problem. Returning a value from a function has no meaning unless you
actually *use* the functions return value somewhere. In this case,

<... onkeypress="return IsNumeric(this);">

would work for MSHTML, however your script code would still be flawed. I
suggest you pass the proprietary local event object reference and then use
`e' instead of `event' in your function:

<... onkeypress="if (typeof event != "undefined") return IsNumeric(event);">

I also suggest you don't annoy your users with an alert() dialog window
every time they type something unexpected but show them the error in a block
element, and make the form validation on submit (and also server-side) instead.

BTW, `IsNumeric' should be `isNumeric' as it does not refer to a constructor.


HTH

PointedEars
 
S

sylver

The focus goes to the field again, and the field value now is 12q.
But I want the field value turns 12. That is, without the last non-
numeric key value pressed. Am I clear enough?

To convert "12q" to "12":
var text = "12q";
var twelve = parseInt(text);
//twelve == 12

or you can user parseFloat()..depends on your needs :D
 
T

Thomas 'PointedEars' Lahn

Thomas said:
fixertool said:
I have a textbox field with numeric validation using a function with
onkeypress event

<input type="text" name="xxx" id="xxx" size="8" maxlength="7"
onkeypress="IsNumeric(this);" />
^
MSHTML which appears to be your target platform does support XHTML. [...]
^
does _not_


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <64e16c6a-0a07-47a4-83fa-51cc57e70985@d1
9g2000prm.googlegroups.com>, Mon, 2 Jun 2008 06:12:03, sylver
To convert "12q" to "12":
var text = "12q";
var twelve = parseInt(text);
//twelve == 12

And what would you expect from "09q" ?

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
E

Evertjan.

Dr J R Stockton wrote on 02 jun 2008 in comp.lang.javascript:
In comp.lang.javascript message <64e16c6a-0a07-47a4-83fa-51cc57e70985@d1
9g2000prm.googlegroups.com>, Mon, 2 Jun 2008 06:12:03, sylver


And what would you expect from "09q" ?

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

Indeed, but just as important is,
that '12p' could easily have ment 120,
and this would be accepted as 12 without informing the user.

I think parseInt() should not be used for parsing user input.
 
F

fixertool

Thanks for all the help. You all were very helpful.

I forgot to say that I was working only for IE (it's an intranet,
using sharepoint too)

Dr.
I put "12p" as an example. I'd expect you understand it's just that.
What do you need? Something like xxxxz (x representing a number, z any
other key)?
Come on, doc.
 
T

Thomas 'PointedEars' Lahn

fixertool said:
Thanks for all the help. You all were very helpful.

You are welcome.
Dr.
I put "12p" as an example. I'd expect you understand it's just that.
What do you need? Something like xxxxz (x representing a number, z any
other key)?
Come on, doc.

Please quote the minimum of what you are replying to and *don't top-post*,
see also <http://jibbering.com/faq/#FAQ2_3> pp.

In his own special way, John was pointing out that parseInt() looks for
prefixes in order to tell the number base it should work on unless you tell
it explicitly what you want. When octal values are supported by the
implementation, parseInt("09q") returns 0 instead of 9 because the leading
`0' indicates octals and the digit `9' does not belong to this number base.
Therefore, you should use parseInt(..., 10) if you want a substring to be
interpreted as a *decimal* number.

See also <http://jibbering.com/faq/#FAQ4_12>.


HTH

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top