Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Capturing Keystrokes that are not permitted within a TextArea

Reply
Thread Tools

Capturing Keystrokes that are not permitted within a TextArea

 
 
@sh
Guest
Posts: n/a
 
      12-20-2005
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!


 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      12-20-2005
@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/
 
Reply With Quote
 
 
 
 
@sh
Guest
Posts: n/a
 
      12-20-2005
> 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,


 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-20-2005
Randy Webb <(E-Mail Removed)> 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.
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      12-20-2005
Christopher Benson-Manica said the following on 12/20/2005 3:15 PM:
> Randy Webb <(E-Mail Removed)> 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/
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-21-2005
Randy Webb <(E-Mail Removed)> 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.
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      12-21-2005
Christopher Benson-Manica said the following on 12/20/2005 9:36 PM:
> Randy Webb <(E-Mail Removed)> 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/
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-21-2005
Randy Webb <(E-Mail Removed)> 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.
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      12-22-2005
Jasen Betts said the following on 12/21/2005 2:16 PM:
> On 2005-12-21, Christopher Benson-Manica <(E-Mail Removed)> wrote:
>
>>Randy Webb <(E-Mail Removed)> 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/
 
Reply With Quote
 
bwucke@gmail.com
Guest
Posts: n/a
 
      12-22-2005
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?)

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
How to add </textarea> within <textarea> tags? frank.moens@gmail.com Javascript 1 07-04-2007 04:00 PM
Unable to serialize the session state. Please note that non-serializable objects or MarshalByRef objects are not permitted when session state mode is 'StateServer' or 'SQLServer'. Mike Larkin ASP .Net 1 05-23-2005 12:33 PM
Multiple definitions of a variable in C++ not permitted Charles L C++ 5 02-08-2005 12:13 PM
Capturing Keystrokes Rob T ASP .Net 1 05-14-2004 11:52 AM



Advertisments