How to detect a Paste action on a textbox?

Discussion in 'Javascript' started by halbenca@gmail.com, Dec 2, 2006.

  1. Guest

    Is this possible with Javascript in anyway? :)
    , Dec 2, 2006
    #1
    1. Advertising

  2. Randy Webb Guest

    Randy Webb, Dec 2, 2006
    #2
    1. Advertising

  3. Guest

    On Dec 2, 6:47 am, wrote:
    > Is this possible with Javascript in anyway? :)


    Yes, it is possible to detect a paste action.
    Here's a simple exemple, preventing to paste in a textarea :

    <html>
    <head>
    <script>
    window.addEventListener('load', function (e)
    {
    document.getElementById('text').addEventListener('keydown', function
    (e)
    {
    if (e.keyCode == 86) //86 is Paste event
    {
    alert('You may not paste here !');
    e.preventDefault();
    }
    document.getElementById('events').innerHTML += e.keyCode + ' ' +
    e.which + '<br />';
    }, false);
    }, false);
    </script>
    </head>

    <body>
    <textarea id="text">
    </textarea>
    <div id="events">
    </div>
    </body>
    </html>
    , Dec 2, 2006
    #3
  4. ASM Guest

    a écrit :
    > On Dec 2, 6:47 am, wrote:
    >> Is this possible with Javascript in anyway? :)

    >
    > Yes, it is possible to detect a paste action.
    > Here's a simple exemple, preventing to paste in a textarea :


    How do you expect to tape 'v' ? or 'V' ?

    Anyway,
    that doesn't work at all !

    report under textbox tells me : 224 224



    --
    Stephane Moriaux et son (moins) vieux Mac déjà dépassé
    Stephane Moriaux and his (less) old Mac already out of date
    ASM, Dec 2, 2006
    #4
  5. Guest

    On Dec 2, 1:18 pm, ASM <>
    wrote:
    > a écrit :
    >
    > > On Dec 2, 6:47 am, wrote:
    > >> Is this possible with Javascript in anyway? :)

    >
    > > Yes, it is possible to detect a paste action.
    > > Here's a simple exemple, preventing to paste in a textarea :How do you expect to tape 'v' ? or 'V' ?

    >
    > Anyway,
    > that doesn't work at all !
    >
    > report under textbox tells me : 224 224
    >
    > --
    > Stephane Moriaux et son (moins) vieux Mac déjà dépassé
    > Stephane Moriaux and his (less) old Mac already out of date


    Oups... You're totally rights... >_<
    I'm sorry i wasn't careful enough about that...

    I'm going to see if I can do something about it :p

    P.S. Ton mac est si vieux ?

    --
    Naixn.
    , Dec 2, 2006
    #5
  6. Guest

    On Dec 2, 1:30 pm, "" <>
    wrote:
    > On Dec 2, 1:18 pm, ASM <>
    > wrote:
    >
    >
    >
    > > a écrit :

    >
    > > > On Dec 2, 6:47 am, wrote:
    > > >> Is this possible with Javascript in anyway? :)

    >
    > > > Yes, it is possible to detect a paste action.
    > > > Here's a simple exemple, preventing to paste in a textarea :How do you expect to tape 'v' ? or 'V' ?

    >
    > > Anyway,
    > > that doesn't work at all !

    >
    > > report under textbox tells me : 224 224

    >
    > > --
    > > Stephane Moriaux et son (moins) vieux Mac déjà dépassé
    > > Stephane Moriaux and his (less) old Mac already out of dateOups... You're totally rights... >_<

    > I'm sorry i wasn't careful enough about that...
    >
    > I'm going to see if I can do something about it :p
    >
    > P.S. Ton mac est si vieux ?
    >
    > --
    > Naixn.


    <script>
    window.addEventListener('load', function (e)
    {
    document.getElementById('text').addEventListener('keydown', function
    (e)
    {
    if (e.keyCode == 86 && e.ctrlKey) //86 is 'v' char key
    {
    e.preventDefault();
    }
    document.getElementById('events').innerHTML += e.keyCode + ' ' +
    e.which + '<br />';
    }, false);
    }, false);
    </script>

    Here it is.
    I forgot to put the "e.ctrlKey" check ( to check wether control key was
    pressed, or not.

    Thing is I don't have any way to try javascript on Safari ( as there's
    no JavaScriptCore port on Linux, apart from KHTML, which begins to
    differs a lot from WebCore... ).
    What is 224 ? Ctrl+V keycode, or V keycode ?

    Is the e.ctrlKey working with safari ? Since I know you have to "pomme
    + v" to paste ( sorry I don't remember the english equivalent for
    'pomme' [ I just know it's not " Apple " :p ] ).

    --
    Naixn.
    , Dec 2, 2006
    #6
  7. ASM Guest

    a écrit :
    > On Dec 2, 1:18 pm, ASM <>
    > wrote:
    >> that doesn't work at all !


    > Oups... You're totally rights... >_<
    > I'm going to see if I can do something about it :p


    > P.S. Ton mac est si vieux ?


    The so noisy G4 MDD (1,25Mhz OS 10.3.9) 2 years old.

    P.S. : qu'est-ce qu'il a le Messenger de ton Mozilla ?
    (pour ne pas gommer la signature de l'envoyeur dans les réponses)

    --
    Stephane Moriaux et son moins vieux Mac déjà dépassé
    Stephane Moriaux and his less old Mac already out of date
    ASM, Dec 2, 2006
    #7
  8. naixn Guest

    ASM wrote :
    > a écrit :
    >> On Dec 2, 1:18 pm, ASM <>
    >> wrote:
    >>> that doesn't work at all !

    >
    >> Oups... You're totally rights... >_<
    >> I'm going to see if I can do something about it :p

    >
    >> P.S. Ton mac est si vieux ?

    >
    > The so noisy G4 MDD (1,25Mhz OS 10.3.9) 2 years old.
    >
    > P.S. : qu'est-ce qu'il a le Messenger de ton Mozilla ?
    > (pour ne pas gommer la signature de l'envoyeur dans les réponses)
    >


    In fact, it's an old one!

    Please refer to my previous post to check the changes I made.
    Maybe you'll also be able to answer my safari-specific questions :)

    P.S. : Tout simplement que j'utilisais google newsgroups.
    C'est maintenant corrigé :)

    --
    Naixn
    http://fma-fr.net
    naixn, Dec 2, 2006
    #8
  9. ASM Guest

    a écrit :
    >
    > Here it is.
    > I forgot to put the "e.ctrlKey" check ( to check wether control key was
    > pressed, or not.


    On Mac Control key is not used to copy/paste/etc
    we use touch Command (Pomme).

    With FireFox, with Command + v and Command + z
    keyCode returns 224
    Command associated with others touchs (x, a, p, ... ) keyCode returns
    correspondant character/touch key code.

    > Thing is I don't have any way to try javascript on Safari


    My tests show Safari doesn't report touch Command (Pomme - Apple)
    nor keyCode as if anything was pressed.

    My test with Opera : Opera doesn't give a special code for
    Command+v Command+c (normal letter key code)

    --
    Stephane Moriaux et son (moins) vieux Mac déjà dépassé
    Stephane Moriaux and his (less) old Mac already out of date
    ASM, Dec 2, 2006
    #9
  10. naixn Guest

    ASM wrote :
    > a écrit :
    >>
    >> Here it is.
    >> I forgot to put the "e.ctrlKey" check ( to check wether control key was
    >> pressed, or not.

    >
    > On Mac Control key is not used to copy/paste/etc
    > we use touch Command (Pomme).
    >
    > With FireFox, with Command + v and Command + z
    > keyCode returns 224
    > Command associated with others touchs (x, a, p, ... ) keyCode returns
    > correspondant character/touch key code.
    >
    >> Thing is I don't have any way to try javascript on Safari

    >
    > My tests show Safari doesn't report touch Command (Pomme - Apple)
    > nor keyCode as if anything was pressed.
    >
    > My test with Opera : Opera doesn't give a special code for
    > Command+v Command+c (normal letter key code)
    >


    Well, let's correct it :

    <script>
    window.addEventListener('load', function (e)
    {
    document.getElementById('text').addEventListener('keydown', function
    (e)
    {
    if ((e.keyCode == 86 && e.ctrlKey) || (e.keyCode == 224 && e.metaKey)
    {
    e.preventDefault();
    }
    document.getElementById('events').innerHTML += e.keyCode + ' ' +
    e.which + '<br />';
    }, false);
    }, false);
    </script>

    Ok now, how does it work with you, with this correction ? :)

    --
    Naixn
    http://fma-fr.net
    naixn, Dec 2, 2006
    #10
  11. wrote:
    > Is this possible with Javascript in anyway? :)


    IE/Win has events for that:
    <http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/events/onbeforepaste.asp>
    <http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/events/onpaste.asp>

    Mozilla has the input event/oninput event handler that fires for any
    kind of input, including a paste operation.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Dec 2, 2006
    #11
  12. Randy Webb Guest

    said the following on 12/2/2006 6:09 AM:
    > On Dec 2, 6:47 am, wrote:
    >> Is this possible with Javascript in anyway? :)

    >
    > Yes, it is possible to detect a paste action.
    > Here's a simple exemple, preventing to paste in a textarea :


    No it doesn't. Open your test page, put the cursor in the textarea, and
    press the key combination of <Shift><Insert>

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Dec 2, 2006
    #12
  13. naixn Guest

    Randy Webb wrote :
    > said the following on 12/2/2006 6:09 AM:
    >> On Dec 2, 6:47 am, wrote:
    >>> Is this possible with Javascript in anyway? :)

    >>
    >> Yes, it is possible to detect a paste action.
    >> Here's a simple exemple, preventing to paste in a textarea :

    >
    > No it doesn't. Open your test page, put the cursor in the textarea, and
    > press the key combination of <Shift><Insert>
    >


    In fact... too bad there's no onPaste event ( apart from IE I mean ).
    Plus, right-click and paste doesn't work either.

    I don't know, then, how to prevent/handle paste in (Gecko/Presto/Webcore)-based
    browsers?

    --
    Naixn
    http://fma-fr.net
    naixn, Dec 2, 2006
    #13
  14. Guest

    <html>
    <head>
    <script>
    window.addEventListener('load', function (e)
    {
    document.getElementById('text').addEventListener('keydown', function
    (e)
    {
    if (e.keyCode == 86) //86 is Paste event
    {
    alert('You may not paste here !');
    e.preventDefault();
    }
    document.getElementById('events').innerHTML += e.keyCode + ' ' +
    e.which + '<br />';
    }, false);
    }, false);

    </script>
    </head>

    <body>
    <textarea id="text">
    </textarea>
    <div id="events">
    </div>
    </body>
    </html>

    When I cut-and-paste this and run on IE, I get a 'object does not
    support this property or metod'. Is something getting cut-off in my
    code above?
    , Dec 2, 2006
    #14
    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. Joe Bloggs
    Replies:
    1
    Views:
    747
    Sudsy
    Aug 3, 2003
  2. rjweytens
    Replies:
    6
    Views:
    16,082
    rjweytens
    Jun 25, 2004
  3. runescience

    Struts mapping action to action???

    runescience, Feb 6, 2006, in forum: Java
    Replies:
    3
    Views:
    1,820
    runescience
    Feb 7, 2006
  4. vyshu
    Replies:
    1
    Views:
    2,547
    Richard Senior
    Apr 27, 2007
  5. John
    Replies:
    0
    Views:
    860
Loading...

Share This Page