Special Characters Processing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Hill

    #1

    Special Characters Processing

    How do I detect the special characters " ' or & when the user pushed
    them.

    I have this code:
    <input type='text' onKeyUp='ck_cha r(this,event);' >

    function ck_char(fld, e)
    {
    var key = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    key = String.fromChar Code(whichCode) ; // Get key value from key code
    alert(key);
    }

    Since each one of these special keys also involve pressing the shift
    key, the only value I get passed back is the last key pressed, which in
    the case of & would always be 7.

    What I'd like to do is detect these special characters when they are
    entered and go ahead and replace them with an appropriate equivalent.

    Mike
  • Evertjan.

    #2
    Re: Special Characters Processing

    Michael Hill wrote on 05 mrt 2004 in comp.lang.javas cript:
    [color=blue]
    > How do I detect the special characters " ' or & when the user pushed
    > them.
    >
    > I have this code:
    > <input type='text' onKeyUp='ck_cha r(this,event);' >
    >
    > function ck_char(fld, e)
    > {
    > var key = '';
    > var whichCode = (window.Event) ? e.which : e.keyCode;
    > key = String.fromChar Code(whichCode) ; // Get key value from key
    > alert(key);
    > }[/color]

    where do you use the "fld" for ?

    =============== =========

    alert(/'"/.test(key))

    will give "true" if either ' or " is found.


    [color=blue]
    > Since each one of these special keys also involve pressing the shift
    > key, the only value I get passed back is the last key pressed, which
    > in the case of & would always be 7.
    >
    > What I'd like to do is detect these special characters when they are
    > entered and go ahead and replace them with an appropriate equivalent.[/color]

    wouldn't we all?


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Michael Hill

      #3
      Re: Special Characters Processing

      [color=blue]
      >
      > where do you use the "fld" for ?[/color]

      Another part of the script that I didn't show.
      [color=blue]
      >
      > =============== =========
      >
      > alert(/'"/.test(key))
      >
      > will give "true" if either ' or " is found.
      >[/color]

      This is giving me "false" for everything I push.

      Mike

      Comment

      • Evertjan.

        #4
        Re: Special Characters Processing

        Michael Hill wrote on 05 mrt 2004 in comp.lang.javas cript:[color=blue][color=green]
        >> alert(/'"/.test(key))
        >>
        >> will give "true" if either ' or " is found.
        >>[/color]
        >
        > This is giving me "false" for everything I push.[/color]

        Ah yes, the key is a number,
        so you will have to convert to a character
        and I forgot the []

        <script>
        key=39 // '
        key= String.fromChar Code(key)
        alert(/['"]/.test(key))

        key=34 // "
        key= String.fromChar Code(key)
        alert(/['"]/.test(key))
        </script>

        tested IE6

        --
        Evertjan.
        The Netherlands.
        (Please change the x'es to dots in my emailaddress)

        Comment

        Working...