mainAppModule.controller('textareaCounterCtrl',function($scope){ $scope.info = { textValue : null, maxSize : null, touched : false, length : null }; $scope.onInit = function(){ $scope.info.maxSize = $("#maxSize").val(); $scope.info.textValue = $("#textValue").val(); $scope.info.length = $scope.info.textValue.replace(/(\r\n|\n|\r)/gm,"").length; }; $scope.onChangeText = function(){ $scope.info.touched = true; }; $scope.$watch("info.textValue",function(newVal,oldVal){ if($scope.info.touched == true){ $newVal = newVal.replace(/(\r\n|\n|\r)/gm,""); if($newVal.length > $scope.info.maxSize){ $scope.info.touched = false; $scope.info.textValue = oldVal; $scope.info.length = oldVal.replace(/(\r\n|\n|\r)/gm,"").length; } else $scope.info.length = $newVal.length; } }); });