Share Blog

Wednesday, June 20, 2018

Disable Cut, Copy and Paste in TextBox using AngularJS in ASP.Net

How to Disable Cut, Copy and Paste in TextBox or TextArea using AngularJS in ASP.Net.

Cut, Copy and Paste operations in TextBox or TextArea can be performed using CTRL button or using Mouse Right Click.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestJavaScript.aspx.cs" Inherits="TestJavaScript" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Disable Cut, Copy and Paste in TextBox using AngularJS</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', [])
app.controller('MyController', function ($scope) {
$scope.AttachEvent = function (control, eventName) {
if (control.addEventListener) {
control.addEventListener(eventName, function (e) { e.preventDefault(); }, false);
} else if (control.attachEvent) {
control.attachEvent('on' + eventName, function () { return false; });
}
};
var controls = document.getElementsByTagName("*");
var regEx = new RegExp("(^| )disable( |$)");
for (var i = 0; i < controls.length; i++) {
if (regEx.test(controls[i].className)) {
$scope.AttachEvent(controls[i], "copy");
$scope.AttachEvent(controls[i], "paste");
$scope.AttachEvent(controls[i], "cut");
}
}
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div ng-app="MyApp" ng-controller="MyController">
Enter Text <asp:TextBox ID="txtbox1" runat="server" CssClass="disable"></asp:TextBox><br />
<br />
Paste Text <asp:TextBox ID="txtbox2" runat="server" CssClass="disable" TextMode="MultiLine" Rows="4" Columns="20"></asp:TextBox>
</div>
</form>
</body>
</html>


No comments:

Post a Comment