Michael J. Calkins
Michael J. Calkins

Reputation: 32663

AngularJS Not counting newlines in var length

When I enter some text and then enter some new lines using test.length does not count new lines in the string. I'm using this text to send SMS messages which are sensitive to all newlines so it all has to count. Any ideas?

Hit enter 4 times and see the counter doesn't go up.

http://codepen.io/clouddueling/pen/HJAfn

Upvotes: 2

Views: 662

Answers (2)

musically_ut
musically_ut

Reputation: 34288

You need an ng-trim="false" to avoid automatic trimming: http://codepen.io/musically_ut/pen/KHBto

The documentation for this is missing but there is an pull request on the way.

Upvotes: 3

MBielski
MBielski

Reputation: 6620

The control characters for CRLF (\r\n) typically don't count in the length of a string. You can detect them with a matching regex and use the number of matches to increment your character count. Something like this:

var crlfCount = mytext.match(/[\n\r]|[\r\n]/g);
linecount += crlfCount.length;

Upvotes: 0

Related Questions