Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cancelling an OnKeyPress event

Status
Not open for further replies.

JulianUK

Programmer
Apr 17, 2002
73
GB
Hi
I'm having trouble cancelling an OnKeyPress event from a function (e.g. when and invalid key is pressed). the event is on an INPUT of type Text.

i've followed a few examples in these archives, but one that appears to be someone elses solution doesn't work for toffee on my machine!

Here is what my code snippit looks like...
------------
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
function validate(event)
{
if (event.keyCode < 48 | event.keyCode > 57)
return false;
}
// -->
</SCRIPT>

ENTER INTEGER:
<input type=&quot;text&quot; name=&quot;NewCode&quot; onkeypress=&quot;validate(event)&quot;></input>
------------------------------
any ideas? (I am using IE5 and do not require NS compatibility).

go on, tell me there's a glaring error - PLEASE!

Thanks
Julian
 
function validate(event)
{
if (event.keyCode < 48 | event.keyCode > 57)
return false;
else return true;

}


and in the onkeypress=&quot;return validate(event)&quot;;

try this Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
The man's a genius!

Yes thats it - I'd missed the 'return' out of the onkeypress def.

Thankyou greatly!

Another glaring error wastes a couple of hours....

Julian
 
if you did want to make it NS6 compatible it wouldn't take much effort and it would open up a lot of possibilities.

function validate(event)
{
var kp=(window.event)?window.event.keyCode:event.which;
return (kp != 48 && kp != 57)
}

Hope this helps.

Gary Haran
 
I hate NN, so i never program with it.

By the stats of all the websites my company allocate, the percentage of Non-IE visits is less than 1%, so it's better, faster and easier to create scripts only for IE.
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
well with that kind of an attitude then maybe you have to understand stats. :)

if you have 14 people visiting your page and half of them are Netscape users but get an error and the page does not work with their browser they turn around. That means that only 7 people will continue on the site and navigate through it.

Each time one of those users hits another page they get a hit for each image and each HTML page downloaded. The only ones that are capable of accessing those pages or want to will be IE only. So your stats will falter and you are actually pushing away many users that could potentially want the information you put online because you want a maximum of people to see.

In all cases pushing for Microsoft only programming is a good way to make sure you one day pay M$ for your right to breathing air or surfing the web. But wait a second. You already pay Microsoft for your browser it isn't free. It was paid by your licence for Windows that you are stuck with anyways when buying a computer.

sets the standards by wich browsers nowadays function. Writing your code for compatibility is not difficult for IE4+, NS6 (NS4 sucks I agree and should be dropped in favor of NS6).

Sorry if I am coming on strong here. :( It's my pet peeve. Gary Haran
 
Our sites don't have much JS, only now we are starting to integrate JS in our code.

when you finish saying that NS4 is bad i have to agree with you. Some years ago, i remembered that i only used NS. Well, ti the release of IE5. After that, my friend, IE is much better and more trusty than NS. With all the problems of NS4 and the opponent is good, people droped NS (that's my point of view), and of course, with the distribution of the IE in the windows, it saves time, and trouble of download netscape.

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top