473,412 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Javascript problem with keyCodes for Delete and '.'

Hi all,
I have a javascript to limit the text entry in a text area after the size reaches 300
(only thing that should be allowed is left arrow,up arrow, right arrow 39,down arrow,delete and backspace ) when i wrote the following script it works fine in firefox but in IE there is a problem. The problem comes in IE from the fact that the keyboard code and the character code overlap each other, and the character code for the period character is 46. It just so happens that the keyboard code for the delete key is also 46. so it allows period which has to be prevented! any help will be appreciated.

And thanks in advance

function stopTyping(field, event, size)
{
var txt = field.value;
var re = /\r\n/g
txt = txt.replace(re, "\n");
if(txt.length >= size)
{
/*
left arrow 37
up arrow 38
right arrow 39
down arrow 40
delete 46
backspace 8
*/
// check for the shift key as well
// This is required as in IE SHIFT+% will return a code of 37...
if(event.shiftKey)
{
return false;
}
else
{
if (event.keyCode != 8 && event.keyCode != 46
&& event.keyCode != 38 && event.keyCode != 39
&& event.keyCode != 40 && event.keyCode != 37)
{
return false;
}
}
}
}
Aug 10 '07 #1
3 11702
epots9
1,351 Expert 1GB
using
Expand|Select|Wrap|Line Numbers
  1. <body onkeydown="alert(event.keyCode);" >
  2.  
in ie7 and firefox 2.0.0.6 i got:
Expand|Select|Wrap|Line Numbers
  1. . -> 190
  2. delete -> 46
  3. del -> 110
  4.  
Aug 10 '07 #2
using
Expand|Select|Wrap|Line Numbers
  1. <body onkeydown="alert(event.keyCode);" >
  2.  
in ie7 and firefox 2.0.0.6 i got:
Expand|Select|Wrap|Line Numbers
  1. . -> 190
  2. delete -> 46
  3. del -> 110
  4.  

Just curious to know is "onKeyPress" that i'm using creating problems with the key code

Thanks in advance,
Goutham
Aug 13 '07 #3
xNephilimx
213 Expert 100+
There's a difference between Firefox and IE when retriving character codes.
In IE, to get the character code with the onkeypress handler you use keyCode, but in Firefox you should use charCode to get it.

Read this http://www.quirksmode.org/js/keys.html#link1
Read the whole article too, it's very useful

May be your problem has something to do with this

Kind regards,
The_Nephilim

Just curious to know is "onKeyPress" that i'm using creating problems with the key code

Thanks in advance,
Goutham
Aug 13 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mike | last post by:
I am not a sql person and could use some help with a delete...here is what I want: I have the following tables/fields (only including necessary fields) answers result_id results result_id
1
by: Sergio del Amo | last post by:
Hi, I post here because, i think that my problem can happen to a lot of people using javascript with xml and xsl and the future feedback can be useful to all of us. I hava an xml file with a...
1
by: midbie | last post by:
i m trying to code a html to work with .mdb file (access) locally it seems that i can connect to the .mdb file by using javascript and ado but i just dont know how to use the recordset object to...
2
by: fareeda | last post by:
Is it possible to use JavaScript with XML and XSL? I tried the following but it does not work.. anything missing? test.xml file: -------------- <?xml version="1.0" encoding="ISO8859-1" ?>...
3
by: sck10 | last post by:
Hello, I am using asp.net 2.0. I have an asp.net page that uses JavaScript to open a popup window. When I use the JavaScript with a content page that has a MasterPage, I get a "return" error...
3
by: Wysiwyg | last post by:
After a server created cookie is processed on the client I want it removed, cleared, or expired in the javascript block but have been unable to do this. If I set a cookie value in the server code...
3
by: COHENMARVIN | last post by:
I have a problem that sounds simple but seems impossible with asp.net. I calculate a variable in the Page_Load event, call it X. Then I want my range validators to fire when a textbox value is...
8
by: DavidB | last post by:
I'm trying to delete a file with File.Delete("c:\Documents and Settings\%username%\Application Data\Microsoft\Excel\excel*.xlb") On doing this I get an Exception "Illegal characters in path" -...
3
by: Peter Afonin | last post by:
Hello, I'd used before methods like RegisterStartupScript with ASP.NET, but this problem I was unable to solve yet using this method. I have a web form with a few web controls - textbox,...
1
by: mohkb | last post by:
If i run my javascript withen html using dreamweaver, it works perfectly well. But when i try to use 'src' command to run the script from another file it doesnt work. Can anybody tell me why. and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.