Connecting Tech Pros Worldwide Forums | Help | Site Map

Capturing Keystrokes that are not permitted within a TextArea

@sh
Guest
 
Posts: n/a
#1: Dec 20 '05
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!



Randy Webb
Guest
 
Posts: n/a
#2: Dec 20 '05

re: Capturing Keystrokes that are not permitted within a TextArea


@sh said the following on 12/20/2005 6:41 AM:[color=blue]
> 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') ;">[/color]

<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/
@sh
Guest
 
Posts: n/a
#3: Dec 20 '05

re: Capturing Keystrokes that are not permitted within a TextArea


> There is more work to it than that, but the approach is there. Write your[color=blue]
> best try at it and post it back here.[/color]

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,


Christopher Benson-Manica
Guest
 
Posts: n/a
#4: Dec 20 '05

re: Capturing Keystrokes that are not permitted within a TextArea


Randy Webb <HikksNotAtHome@aol.com> wrote:
[color=blue]
> var myList = new Object();
> myList['myTextArea1'] = ['a','<','>','b','u','i']
> myList['myTextArea2'] = ['<','>','b','u','i'][/color]

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.
Randy Webb
Guest
 
Posts: n/a
#5: Dec 20 '05

re: Capturing Keystrokes that are not permitted within a TextArea


Christopher Benson-Manica said the following on 12/20/2005 3:15 PM:[color=blue]
> Randy Webb <HikksNotAtHome@aol.com> wrote:
>
>[color=green]
>>var myList = new Object();
>>myList['myTextArea1'] = ['a','<','>','b','u','i']
>>myList['myTextArea2'] = ['<','>','b','u','i'][/color]
>
>
> Is that somehow superior to
>
> myList[ 'myTextArea1' ]='a<>bui';
> myList[ 'myTextArea2' ]='a<>bui';[/color]

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/
Christopher Benson-Manica
Guest
 
Posts: n/a
#6: Dec 21 '05

re: Capturing Keystrokes that are not permitted within a TextArea


Randy Webb <HikksNotAtHome@aol.com> wrote:
[color=blue]
> In the sense that you would have to split and then loop through the
> array whereas the first is already an array.[/color]

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.
Randy Webb
Guest
 
Posts: n/a
#7: Dec 21 '05

re: Capturing Keystrokes that are not permitted within a TextArea


Christopher Benson-Manica said the following on 12/20/2005 9:36 PM:[color=blue]
> Randy Webb <HikksNotAtHome@aol.com> wrote:
>
>[color=green]
>>In the sense that you would have to split and then loop through the
>>array whereas the first is already an array.[/color]
>
>
> I was thinking charAt(), obviating the need to split() first.[/color]

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/
Christopher Benson-Manica
Guest
 
Posts: n/a
#8: Dec 21 '05

re: Capturing Keystrokes that are not permitted within a TextArea


Randy Webb <HikksNotAtHome@aol.com> wrote:
[color=blue]
> 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.[/color]

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.
Jasen Betts
Guest
 
Posts: n/a
#9: Dec 21 '05

re: Capturing Keystrokes that are not permitted within a TextArea


On 2005-12-21, Christopher Benson-Manica <ataru@nospam.cyberspace.org> wrote:[color=blue]
> Randy Webb <HikksNotAtHome@aol.com> wrote:
>[/color]
[color=blue][color=green]
>> In the sense that you would have to split and then loop through the
>> array whereas the first is already an array.[/color]
>
> I was thinking charAt(), obviating the need to split() first.[/color]

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
Randy Webb
Guest
 
Posts: n/a
#10: Dec 22 '05

re: Capturing Keystrokes that are not permitted within a TextArea


Jasen Betts said the following on 12/21/2005 2:16 PM:[color=blue]
> On 2005-12-21, Christopher Benson-Manica <ataru@nospam.cyberspace.org> wrote:
>[color=green]
>>Randy Webb <HikksNotAtHome@aol.com> wrote:
>>[/color]
>
>[color=green][color=darkred]
>>>In the sense that you would have to split and then loop through the
>>>array whereas the first is already an array.[/color]
>>
>>I was thinking charAt(), obviating the need to split() first.[/color]
>
>
> Mozilla allows somestring[index] as another way of expressing
> somestring.charAt(index).[/color]

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.
[color=blue]
> I don't know if that's standard or not, it's just something I've noticed.[/color]

"Standard" as in ECMA Standards, or, standard as in standard behavior?

The first is irrelevant, the second is no.
[color=blue]
> I haven't investigated the ways in which strings differ from arrays.[/color]

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/
bwucke@gmail.com
Guest
 
Posts: n/a
#11: Dec 22 '05

re: Capturing Keystrokes that are not permitted within a TextArea


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?)

@sh
Guest
 
Posts: n/a
#12: Dec 22 '05

re: Capturing Keystrokes that are not permitted within a TextArea


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.



<bwucke@gmail.com> wrote in message
news:1135255934.821343.174390@g47g2000cwa.googlegr oups.com...[color=blue]
> 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?)
>[/color]


@sh
Guest
 
Posts: n/a
#13: Dec 22 '05

re: Capturing Keystrokes that are not permitted within a TextArea


> Might be worth testing to see if using charAt is faster than the array[color=blue]
> method. Off the top of my head, the array method should be faster.[/color]

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?


Kevin Darling
Guest
 
Posts: n/a
#14: Dec 22 '05

re: Capturing Keystrokes that are not permitted within a TextArea


@sh wrote:[color=blue]
> 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') ;">[/color]

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

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

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

Kev

Randy Webb
Guest
 
Posts: n/a
#15: Dec 22 '05

re: Capturing Keystrokes that are not permitted within a TextArea


@sh said the following on 12/22/2005 9:39 AM:[color=blue][color=green]
>>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.[/color]
>
>
> 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?[/color]

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/
Closed Thread


Similar JavaScript / Ajax / DHTML bytes