How to keep leading and trailing spaces when using an input tag?

I play with Angular and am writing a Regex tester. The problem is that missing spaces are truncated when entering data. Jsfiddle example here :

So, when the page loads, I have RegEx "^ \ d + $". test ("123"), which results in "No match", but if you enter an additional top or end space in the "Candidate" field:

  • Master and trailing spaces are removed from my variable
  • As a result, the β€œMatch” changes.

Here is my HTML:

<div id='ng:app' class='ng-app: myApp' ng-app='myApp'> <div ng-controller="Controller">{{addTodo2()}} <form novalidate class="simple-form">Pattern: <input type="text" ng-model="pattern" />Candidate: <input type="text" ng-model="candidate" /> <br />.{{candidate}}. <br>.{{candidate2}}.</form> </div> </div> 

And here is the corresponding JavaScript:

 function Controller($scope) { $scope.pattern = "^\\d+$"; $scope.candidate = " 123 "; $scope.candidate2 = " 123 "; $scope.addTodo2 = function () { var str = "Javascript is an interesting scripting language"; var re = new RegExp($scope.pattern, "g"); var result = re.test($scope.candidate); if (result) { return "Match22"; } else { return "No Match22"; }; }; } var myapp = angular.module('myApp', []); 
+6
source share
1 answer

Updated script, added ng-trim = "false" to input tags

http://jsfiddle.net/T2zuV/12/

 <div id='ng:app' class='ng-app: myApp' ng-app='myApp'> <div ng-controller="Controller">{{addTodo2()}} <form novalidate class="simple-form">Pattern: <input type="text" ng-model="pattern" ng-trim="false"/>Candidate: <input type="text" ng-model="candidate" ng-trim="false"/> <br />.{{candidate}}. <br>.{{candidate2}}.</form> </div> </div> 
+11
source

Source: https://habr.com/ru/post/949797/


All Articles