javascript - Capturing mouseclick on an asp.net Textbox -


i've got popup box allows user type brief (50 character) note. need figure out how prevent user copy/pasting note using right mouse click.

i looked events available textbox, , onkeyup , onkeydown not in list have effect.

i have following javascript captures keystrokes:

<!--//**********************************     // comment character count     //**********************************  --> <script type="text/javascript">      function textcounter(field, countfield, maxlimit) {      if (field.value.length > maxlimit)         field.value = field.value.substring(0, maxlimit);      else         countfield.value = maxlimit - field.value.length;      }  </script> 

and textbox calls above function when key pressed:

<asp:textbox id="txtcommentbox" textmode="multiline" cssclass="textbox" wrap="true"  height="70px" width="270px" font-size="small" rows="3" runat="server"   onkeyup="textcounter(this, this.form.remlen, 50);"  onkeydown="textcounter(this, this.form.remlen, 50);" /> 

everything works awesome, except when right-clicks , chooses "paste", bypasses check because no keys pressed. ideas on how trap that? there undocumented onmouseclick event can call or something?

first, add onpaste event textbox:

<asp:textbox id="txtcommentbox" textmode="multiline" cssclass="textbox" wrap="true"  height="70px" width="270px" font-size="small" rows="3" runat="server"   onkeyup="textcounter(this, this.form.remlen, 50);"  onkeydown="textcounter(this, this.form.remlen, 50);" onpaste="textcounter(this, this.form.remlen, 50);" /> 

next, adapt fabrício matté's solution handle timing difference between onpaste firing , value being populated:

function textcounter(field, countfield, maxlimit) {     settimeout(function () {         if (field.value.length > maxlimit)             field.value = field.value.substring(0, maxlimit);         else             countfield.value = maxlimit - field.value.length;      }, 0); } 

Comments