Capture Key, only allow numbers

L

Leroy

I have this function:

function ValidateIntKey()
{
var Key;
var CharKey;
Key = event.keyCode;
alert(Key);

//CharKey = String.charCodeAt(Key);
alert(String.fromCharCode(Key));
CharKey = String.fromCharCode(Key)
if (!isInteger(CharKey))
{
alert("no integer");
}
}

Where i have "alert("no integer");" i want to make the keyCode null so
that it doesn't reach the textbox this function is attached to. How
do i do this? I tried:

event.keyCode = 0; but this did not work. Any other ideas?
 
P

Philip Ronan

I have this function:

function ValidateIntKey()
{
var Key;
var CharKey;
Key = event.keyCode;
alert(Key);

//CharKey = String.charCodeAt(Key);
alert(String.fromCharCode(Key));
CharKey = String.fromCharCode(Key)
if (!isInteger(CharKey))
{
alert("no integer");
}
}

Where i have "alert("no integer");" i want to make the keyCode null so
that it doesn't reach the textbox this function is attached to. How
do i do this? I tried:

event.keyCode = 0; but this did not work. Any other ideas?

Has it occurred to you that this function is going to annoy the crap out of
everyone? We all make typing mistakes from time to time; that's why
keyboards have a delete key. If your web page throws an alert at me every
time I make a tiny slip of the finger then I'm not going to stay there very
long.

Why not do your form validation the old-fashioned way and check for integers
in an onsubmit() event?

Phil
 
L

Lee

Philip Ronan said:
Has it occurred to you that this function is going to annoy the crap out of
everyone? We all make typing mistakes from time to time; that's why
keyboards have a delete key. If your web page throws an alert at me every
time I make a tiny slip of the finger then I'm not going to stay there very
long.

Why not do your form validation the old-fashioned way and check for integers
in an onsubmit() event?

Absolutely! Even if you do away with the alert and simply block
all non-integer keystrokes, you're going to send people away angry.

Imagine that I'm trying to type "123" and accidentally hit "12w".
I may not even be looking at the screen when I realize that I've
hit the wrong key and automatically hit the backspace. If you've
blocked the "w" keystroke, I've just deleted the "2".
 
L

Leroy Stanislowski

I am not looking for an analysis of what i am doing. I am just looking
for an answer to my problem, please.

I understand what everyone is saying about the annoyance, but i have a
reason for doing this and it is not relevant to getting an answer to my
problem.

Can someone please tell me how i can intercept a keystroke an replace it
with nothing? Thank you in advance
 
L

Leroy Stanislowski

Found the answer.

function ValidateIntKey()
{
var Key;
var CharKey;
Key = event.keyCode;
//alert(String.fromCharCode(Key));
CharKey = String.fromCharCode(Key)
if (!isInteger(CharKey))
{
event.returnValue = false;
}
}
 
L

Leroy Stanislowski

Found the answer.

function ValidateIntKey()
{
var Key;
var CharKey;
Key = event.keyCode;
//alert(String.fromCharCode(Key));
CharKey = String.fromCharCode(Key)
if (!isInteger(CharKey))
{
event.returnValue = false;
}
}

This only works on keypress, not keyup or keydown.
 
G

George Hester

Leroy Stanislowski said:
I am not looking for an analysis of what i am doing. I am just looking
for an answer to my problem, please.

Good luck Leroy
 
T

Thomas 'PointedEars' Lahn

Leroy said:
I am not looking for an analysis of what i am doing. I am just looking
for an answer to my problem, please.

Your problem is that you have not pressed the Power button
of the computer you are abusing. You're welcome.


PointedEars
 
R

Randy Webb

Thomas said:
Leroy Stanislowski wrote:




Your problem is that you have not pressed the Power button
of the computer you are abusing. You're welcome.

And to think it only took you 13 days to come up with that garbage. Wow.
You are starting to really impress me. Wanna buy some Ocean Front
property in Arizona? I have a deal just for you........

P.S. ExLax might be of help to you, since you are full of s**t.
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top