delete vs period

T

Tim Slattery

I'm trying to make a text input field accept only digits and period,
and only one period. I also want things like delete, left/right arrow,
backspace, home, and end to work.

What I've found is that the event object is different in IE and
Firefox. In IE, I have to use the keyCode property to find out what
was pressed, in Firefox I use charCode.

But...46 is the code for period. But in Firefox's charCode property
that seems also to be the code for the "Delete" key.

When I get a period, I check the value of the field to determine
whether there's already a period. If there is, I don't allow this
character (return false). Since the charCodes are the same in FF, the
script now disables the Delete key when there's a period in the field.

So how do I tell the difference between period and delete in Firefox?
 
S

Swifty

But in Firefox's charCode property
that seems also to be the code for the "Delete" key.

There's something very off going on. I agree that 46 (0X2E) is a
period.

In my keyboard codes scanner, delete comes in as 224 (0XE0) followed
by 83 (0X53)

For the "Del" in the numeric keypad that's 0X0053
 
E

Eric Bednarz

Tim Slattery said:
I'm trying to make a text input field accept only digits and period,

So manipulate the value of the field when the keypress event fires.

That also protects you from crippling the user interface; on OS X I use
ctrl+d for delete; incidentally, my keyboard doesn't have a delete key.
 
A

Antony Scriven

I'm trying to make a text input field accept only digits
and period, and only one period. I also want things like
delete, left/right arrow, backspace, home, and end to
work.

What I've found is that the event object is different in
IE and Firefox. In IE, I have to use the keyCode property
to find out what was pressed, in Firefox I use charCode.

But...46 is the code for period. But in Firefox's
charCode property that seems also to be the code for the
"Delete" key.

When I get a period, I check the value of the field to
determine whether there's already a period. If there is,
I don't allow this character (return false). Since the
charCodes are the same in FF, the script now disables the
Delete key when there's a period in the field.

So how do I tell the difference between period and delete
in Firefox?

It sounds like you're using onkeypress. Try onkeydown
instead. Alternatively you could let the user input whatever
they like, but display an error if they try to submit the
form (if that's the scenario). That's easier. --Antony
 
T

Tim Slattery

Tim Slattery said:
I'm trying to make a text input field accept only digits and period,
and only one period. I also want things like delete, left/right arrow,
backspace, home, and end to work.

What I've found is that the event object is different in IE and
Firefox. In IE, I have to use the keyCode property to find out what
was pressed, in Firefox I use charCode.

But...46 is the code for period. But in Firefox's charCode property
that seems also to be the code for the "Delete" key.

After some more experimentation:

onkeypress does not fire in IE for non-character keys like delete,
home, end, arrows, etc. It does fire for those keys in Firefox.

If Firefox, when a character key is pressed (digits, letters, symbols,
something that appears on the screen), charCode contains the
identifying code, keyCode is zero. When a non-character key is
pressed, keyCode has the code and charCode is zero.

So when keyCode == 46 it's the delete key, but when charCode == 46,
it's the "." key.

I'm using IE8 and FF 3.6.16 (I know, I know. Blame my employer, I'm
lucky to have any version of FF at all, even though I'm writing web
systems for the public.)
 
D

Dr J R Stockton

In comp.lang.javascript message <7fdedd50-754e-491c-842e-baea1de726cd@4g
2000yqu.googlegroups.com>, Fri, 28 Oct 2011 09:27:31, Antony Scriven
It sounds like you're using onkeypress. Try onkeydown
instead. Alternatively you could let the user input whatever
they like, but display an error if they try to submit the
form (if that's the scenario). That's easier. --Antony

It depends a little on the application.

A professional data entry clerk may like to have characters typed in one
by one and immediately checked for acceptability.

That, implemented literally, would not help those who enter with
copy'n'paste or drag'n'drop.

Sometimes, users really benefit from being able to copy anything into
the field, and then editing until right, then signifying completion, at
which stage validation can be appropriate.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top