Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Copy/Paste/Right Click in mvc TextBox

I have a textbox which accepts numeric value.This is handled using javascript. I'd like to disable copy, paste and right click functionalities for the text box. Any help would be appreciated.

@Html.TextBoxFor(model => model.Days, new { @class = "input_box", @id = "txtDays", @onkeydown = "javascript:NumberOnly(this,event)"})   
like image 593
Mukesh Kalgude Avatar asked Dec 24 '22 18:12

Mukesh Kalgude


2 Answers

You can do it by using oncopy and onpaste event:

 @Html.TextBoxFor(model => model.Days, 
                  new { 
                        @class = "input_box", 
                        id = "txtDays",
                        oncopy="return false", 
                        onpaste="return false" 
                      }
                   ) 

You may want to visit this article which explains couple of ways to do it.

like image 71
Ehsan Sajjad Avatar answered Jan 09 '23 02:01

Ehsan Sajjad


Following code might help you out.

$('#txtDays').bind("cut copy paste",function(e) {
      e.preventDefault(); 
});
like image 22
JJRS Avatar answered Jan 09 '23 03:01

JJRS