Angular: Empty Fiddle

http://angularjs.org/

by Pratik Bhattachary

HTML

<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<div ng-controller="MyCtrl">
  <label>Enter your text here to check for non-breaking space: </label>
  <br/>
  <input type="text" ng-model="myText" ng-change="checkForNonBreaking()" style="width: 80%;" />
  <br/>
  <div ng-show="isNonBreakingPresent"  style="color: red; font-weight: bold;">
    Non-Breaking Character has been detected
    <button ng-click="replaceNonBreakingCharacters()">
      Remove Non-Breaking Spaces
    </button>
  </div>
  <div ng-hide="isNonBreakingPresent">
    No Non-Breaking Space is present
  </div>

</div>

JavaScript

var myApp = angular.module('myApp', []);


function MyCtrl($scope) {
  $scope.isNonBreakingPresent = false;

  $scope.checkForNonBreaking = function() {
    var positions = "";
    $scope.isNonBreakingPresent = false;
    for (var i = 0, len = $scope.myText.length; i < len; i++) {
      if ($scope.myText[i] === '\xa0') {
        positions += i + ", ";
        $scope.isNonBreakingPresent = true;
      }

    }
    if (positions.length > 1)
      alert("Non-breaking space detected at position: " + positions);
  }

  $scope.replaceNonBreakingCharacters = function() {
    var replaced = $scope.myText.replace(/\xa0/g, ' ')
    $scope.myText = replaced;
    $scope.checkForNonBreaking();
  }
}