Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Is there a key buffer in JavaScript?
Old 05-27-2008, 12:20 PM Is there a key buffer in JavaScript?
Super Talker

Posts: 116
Trades: 0
Hey all,

I'm writing an event listener that is trapping for certain keys (single quote & ampersand mainly) in a text input field.

I'm able to determine the key press through the keyCode. If an ampersand or single quote comes up, I give an alert box that those entries are not valid.

However, after the alert, it still writes the single quote or ampersand into text field.

I tried to get the length of the field and do a substring of the length -1, hoping to kill the ampersand or single quote. But it doesn't work. It cuts off a legitimate character and then writes in the single quote or ampersand.

I'm suspecting there's some sort of key buffer, but I can't seem to locate information on the web.

Here is the function code. I only wrote it for the ampersand.

function checkText(evt,lcID)
{
var charCode = (evt.which) ? evt.which : event.keyCode
switch(charCode)
{
case 38:
{ alert("Ampersands are not allowed");
var lcText = document.getElementById(lcID).value;
var lnLen = document.getElementById(lcID).value.length;
lnLen = lnLen-2;
document.getElementById(lcID).value = lcText.substr(0,lnLen);
break; }
case 39:
{ alert("Single quotes are not allowed");
var lcText = document.getElementById(lcID).value;
break; }
default: { break; }
}
}

Hope anyone can shed some light.

Thanks
Donna
DonnaZ is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-27-2008, 12:28 PM Re: Is there a key buffer in JavaScript?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Please use code tags.

In your HTML you could add something like this:

HTML Code:
<input type="text" ... onKeyUp="return checkThis();" />
Then if the key is considered invalid, return false to disable the keystroke.

Be advised that this won't ensure one has not entered those keys -- 2 clicks to disabled javascript and I bypass your security. Be sure to sanitize data on the server too.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-29-2008, 12:39 AM Re: Is there a key buffer in JavaScript?
Experienced Talker

Posts: 39
Name: Andrew
Trades: 0
Jeremy's approach is the best one, but I just wanted to add that the reason your code isn't working is that you're trapping the wrong event. KeyDown and KeyPress both fire before characters are added to the textbox, so the offending quote won't be there when your handler triggers. If you hooked into KeyUp instead it would work.

Edit: That is to say, it will almost work. KeyUp only fires once per key stroke, no matter how long you hold the key down...
__________________

Please login or register to view this content. Registration is FREE
- Pole dancing evolved

Last edited by meloncholy; 05-29-2008 at 12:48 AM..
meloncholy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Is there a key buffer in JavaScript?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11833 seconds with 12 queries