Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do preserve leading and trailing whitespace when using an input tag?

Tags:

html

angularjs

I am playing with Angular and writing a Regex tester. Problem is leading whitespace is trimmed when I enter data. See example jsfiddle here:

So when the page loads I have the RegEx "^\d+$".test(" 123 ") which results in "No Match", But if you enter an extra leading or trailing space in the Candidate box:

  1. The leading and trailing spaces are removed from my variable
  2. The result changes "Match"

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 associated 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', []);
like image 638
MrSteve Avatar asked Jul 19 '13 06:07

MrSteve


People also ask

How do you remove leading and trailing white spaces from input text?

We can eliminate the leading and trailing spaces of a string in Java with the help of trim().

What is leading and trailing whitespace?

A leading space is a space that is located before the first character (letter, number, punctuation mark) in a text entry field. A trailing space is a space that is located after the final character in a text entry field.

How do you fix a trailing whitespace error?

Use your editor to find the end of the line and backspace. Many modern text editors can also automatically remove trailing whitespace from the end of the line, for example every time you save a file. In emacs: C-M-% <space> + $ then press return twice.

Which function used to remove all leading and trailing whitespace in string?

You can use the STRIP function to remove both the leading and trailing spaces from the character strings.


1 Answers

Updated the fiddle, added ng-trim="false" to the 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>
like image 154
shaunhusain Avatar answered Sep 18 '22 18:09

shaunhusain