473,386 Members | 1,668 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,386 software developers and data experts.

Capturing Keystrokes that are not permitted within a TextArea

@sh
Has anyone a function handy that I can apply to various textboxes within a
form, each textbox will permit most characters but I want to ban certain
characters from each textbox.

Therefore I need a function that I can put into the <text area> tag of each
box, something like...

<text area onKeyPress="BanCharacters('a','<','>','b','u','i') ;">

Is this possible easily?

Cheers!
Dec 20 '05 #1
14 1730
@sh said the following on 12/20/2005 6:41 AM:
Has anyone a function handy that I can apply to various textboxes within a
form, each textbox will permit most characters but I want to ban certain
characters from each textbox.

Therefore I need a function that I can put into the <text area> tag of each
box, something like...

<text area onKeyPress="BanCharacters('a','<','>','b','u','i') ;">


<textarea onkeypress="banCharacters(this,'character list here');
name="myTextArea1">

Then banCharacters could check the keystrokes and compare against the
list. If its in the list, return false.

Might be better to have an array or simple object for each textarea
predefined so that you don't have to worry about syntax or arguments
list length.

Even simpler/better would be an array/object that had a list of all of
them. Something like this:

var myList = new Object();
myList['myTextArea1'] = ['a','<','>','b','u','i']
myList['myTextArea2'] = ['<','>','b','u','i']
and so on.

function banCharacters(textAreaRef){
//myList[textAreaRef.name] will give you a reference to the list.
//loop through that list and compare to the keypress.
//return false if it matches
}

There is more work to it than that, but the approach is there. Write
your best try at it and post it back here.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 20 '05 #2
@sh
> There is more work to it than that, but the approach is there. Write your
best try at it and post it back here.


Thanks Randy, I'll give it a go ;o) I've got to finish the application
first so I'll get there, then go back and cleanup with the Javascript
verification bits like this.

Cheers,
Dec 20 '05 #3
Randy Webb <Hi************@aol.com> wrote:
var myList = new Object();
myList['myTextArea1'] = ['a','<','>','b','u','i']
myList['myTextArea2'] = ['<','>','b','u','i']


Is that somehow superior to

myList[ 'myTextArea1' ]='a<>bui';
myList[ 'myTextArea2' ]='a<>bui';

?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 20 '05 #4
Christopher Benson-Manica said the following on 12/20/2005 3:15 PM:
Randy Webb <Hi************@aol.com> wrote:

var myList = new Object();
myList['myTextArea1'] = ['a','<','>','b','u','i']
myList['myTextArea2'] = ['<','>','b','u','i']

Is that somehow superior to

myList[ 'myTextArea1' ]='a<>bui';
myList[ 'myTextArea2' ]='a<>bui';


In the sense that you would have to split and then loop through the
array whereas the first is already an array.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 20 '05 #5
Randy Webb <Hi************@aol.com> wrote:
In the sense that you would have to split and then loop through the
array whereas the first is already an array.


I was thinking charAt(), obviating the need to split() first.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 21 '05 #6
Christopher Benson-Manica said the following on 12/20/2005 9:36 PM:
Randy Webb <Hi************@aol.com> wrote:

In the sense that you would have to split and then loop through the
array whereas the first is already an array.

I was thinking charAt(), obviating the need to split() first.


Might be worth testing to see if using charAt is faster than the array
method. Off the top of my head, the array method should be faster.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 21 '05 #7
Randy Webb <Hi************@aol.com> wrote:
Might be worth testing to see if using charAt is faster than the array
method. Off the top of my head, the array method should be faster.


You're probably right, although the string method is a little cleaner
IMHO. I doubt the performance difference would be noticable for most
applications.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dec 21 '05 #8
On 2005-12-21, Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Randy Webb <Hi************@aol.com> wrote:

In the sense that you would have to split and then loop through the
array whereas the first is already an array.


I was thinking charAt(), obviating the need to split() first.


Mozilla allows somestring[index] as another way of expressing
somestring.charAt(index). I don't know if that's standard or not,
it's just something I've noticed.

I haven't investigated the ways in which strings differ from arrays.

Bye.
Jasen
Dec 21 '05 #9
Jasen Betts said the following on 12/21/2005 2:16 PM:
On 2005-12-21, Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Randy Webb <Hi************@aol.com> wrote:

In the sense that you would have to split and then loop through the
array whereas the first is already an array.


I was thinking charAt(), obviating the need to split() first.

Mozilla allows somestring[index] as another way of expressing
somestring.charAt(index).


Opera nor IE follow that construct. I remember seeing that a while back
and never bothered with it because of lack of support in other browsers.
I don't know if that's standard or not, it's just something I've noticed.
"Standard" as in ECMA Standards, or, standard as in standard behavior?

The first is irrelevant, the second is no.
I haven't investigated the ways in which strings differ from arrays.


The difference in Strings and Arrays is very significant.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 22 '05 #10
Smart users will cut&paste their "illegal chars" using context menu,
this way keypress won't be triggered. Better use onChange. And then,
still testing the character at the end of textarea misses its purpose
as the user may paste a larger block of text or start editing in the
middle. Instead of arguing which one is faster, looping over string
chars or array element, I suggest fast system
onKeyUp="this.value=this.value.replace(/[f0rbIdden]/g,'')";

(yes, I write in Perl, why?)

Dec 22 '05 #11
@sh
Fair point but this application is secure anyway, the only reason we're
blocking these characters is because it makes the end result a bit tricker,
its not necessarily for security.

<bw****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Smart users will cut&paste their "illegal chars" using context menu,
this way keypress won't be triggered. Better use onChange. And then,
still testing the character at the end of textarea misses its purpose
as the user may paste a larger block of text or start editing in the
middle. Instead of arguing which one is faster, looping over string
chars or array element, I suggest fast system
onKeyUp="this.value=this.value.replace(/[f0rbIdden]/g,'')";

(yes, I write in Perl, why?)

Dec 22 '05 #12
@sh
> Might be worth testing to see if using charAt is faster than the array
method. Off the top of my head, the array method should be faster.


Interesting, just out of curiosity how on earth do you test the speed
difference between two methods when they both would execute so quickly
anyway? I suppose you set some counter method or something and then pop it
into an Alert?
Dec 22 '05 #13
@sh wrote:
Has anyone a function handy that I can apply to various textboxes within a
form, each textbox will permit most characters but I want to ban certain
characters from each textbox.

Therefore I need a function that I can put into the <text area> tag of each
box, something like...

<text area onKeyPress="BanCharacters('a','<','>','b','u','i') ;">


function onpress()
{
var sForbidden = "a<>bui";

var char = String.fromCharCode(event.keyCode);

if (sForbidden.indexOf(char) > -1)
{
event.returnValue = false;
}
}

Kev

Dec 22 '05 #14
@sh said the following on 12/22/2005 9:39 AM:
Might be worth testing to see if using charAt is faster than the array
method. Off the top of my head, the array method should be faster.

Interesting, just out of curiosity how on earth do you test the speed
difference between two methods when they both would execute so quickly
anyway? I suppose you set some counter method or something and then pop it
into an Alert?


Perform 50,000 or so loops and time them.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 22 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bilal | last post by:
Hi, Is there any way to capture the action Ctrl+N (whether it in a hidden button or keyword - doesn't matter) in html, javascript or php? I appreciate any suggestions. Thank you and kind regards...
3
by: coolsti | last post by:
Can someone help me enhance this code snippet which works only for IE so that it will also work for Firefox? I have been trying all sorts of things and have gotten to where I can capture the...
33
by: Joerg Schuster | last post by:
Hello, Python regular expressions must not have more than 100 capturing groups. The source code responsible for this reads as follows: # XXX: <fl> get rid of this limitation! if...
1
by: Pjotr Wedersteers | last post by:
Hi I want to write a small prog/applet that does nothing but the following: Display icons representing the keys pressed on the keyboard in a small "always on top" but transparent window. The...
1
by: Rob T | last post by:
In the past, I've made several poor attempts at capturing keyboard strokes with JS. Mostly, I would want to hit something like ALT-H or F1 for a help screen.... Hitting the Alt key usually...
2
by: jbigham | last post by:
Hello, I'd like to capture key events using javascript, but don't want to process such events when the user is typing into an input box or into a textarea. As an example, gmail has a feature...
2
by: David McDivitt | last post by:
Another fellow here must modify an application. On the web page, pressing the enter key causes the form to submit. What users want is to remove enter key functionality from within a textarea and...
5
by: konryd | last post by:
I want to know when a user scrolls the textarea. Since there is no such an event, I need to scroll all the ways scrolling might be invoked, that is: * by pressing keys * by moving mouse * by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.