Capturing Keystrokes that are not permitted within a TextArea

Discussion in 'Javascript' started by @sh, Dec 20, 2005.

  1. @sh

    @sh Guest

    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!
     
    @sh, Dec 20, 2005
    #1
    1. Advertising

  2. @sh

    Randy Webb Guest

    @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/
     
    Randy Webb, Dec 20, 2005
    #2
    1. Advertising

  3. @sh

    @sh Guest

    > 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,
     
    @sh, Dec 20, 2005
    #3
  4. Randy Webb <> 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.
     
    Christopher Benson-Manica, Dec 20, 2005
    #4
  5. @sh

    Randy Webb Guest

    Christopher Benson-Manica said the following on 12/20/2005 3:15 PM:
    > Randy Webb <> 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/
     
    Randy Webb, Dec 20, 2005
    #5
  6. Randy Webb <> 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.
     
    Christopher Benson-Manica, Dec 21, 2005
    #6
  7. @sh

    Randy Webb Guest

    Christopher Benson-Manica said the following on 12/20/2005 9:36 PM:
    > Randy Webb <> 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/
     
    Randy Webb, Dec 21, 2005
    #7
  8. Randy Webb <> 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.
     
    Christopher Benson-Manica, Dec 21, 2005
    #8
  9. @sh

    Randy Webb Guest

    Jasen Betts said the following on 12/21/2005 2:16 PM:
    > On 2005-12-21, Christopher Benson-Manica <> wrote:
    >
    >>Randy Webb <> 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/
     
    Randy Webb, Dec 22, 2005
    #9
  10. @sh

    Guest

    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, 2005
    #10
  11. @sh

    @sh Guest

    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.



    <> wrote in message
    news:...
    > 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, Dec 22, 2005
    #11
  12. @sh

    @sh Guest

    > 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?
     
    @sh, Dec 22, 2005
    #12
  13. @sh

    Lee Guest

    @sh said:
    >
    >> 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?


    var count = BIGNUMBER;
    var start = new Date();
    while ( count --> 0 ) {
    // do something that is very fast;
    }
    var finish = new Date();
    alert( (finish.getTime() - start.getTime()) / BIGNUMBER );
     
    Lee, Dec 22, 2005
    #13
  14. @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
     
    Kevin Darling, Dec 22, 2005
    #14
  15. @sh

    Randy Webb Guest

    @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/
     
    Randy Webb, Dec 22, 2005
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob T

    Capturing Keystrokes

    Rob T, May 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    827
    Vidar Petursson
    May 14, 2004
  2. Charles L
    Replies:
    5
    Views:
    1,269
    Ron Natalie
    Feb 8, 2005
  3. Tristin Davis
    Replies:
    4
    Views:
    346
    Tristin Davis
    Jul 4, 2008
  4. Sam

    Operation not permitted

    Sam, Feb 25, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    222
    James Willmore
    Mar 6, 2004
  5. Replies:
    1
    Views:
    343
    Bart Van der Donck
    Jul 4, 2007
Loading...

Share This Page