2
<html>
<head>
</head>
<body>
    <textarea maxlength="10"></textarea>
</body>
</html>

I am encountering an issue that counts return key as 2 characters. I have applied regex also to solve this issue but unable to do that. The regex I applied is (/(?:\r\n|\r|\n)/g, '\n')

Parisah
  • 23
  • 1
  • 3

1 Answers1

2

The carriage returns in Chrome are \r\n, which are considered two characters (\r and \n).

I would recommend you to take a look in the following questions:

The last link can provide an explanation as why limiting the length by HTML attributes make the new line count as two characters (which is the real length) while JavaScript code considers them to be a single character.

If you really need to worry about line breaks and character counting, I would suggest you to replace the length validation in HTML by a JavaScript validation in your AngularJS application, as you would be able to count line breaks as a single character, as desired.

Community
  • 1
  • 1
Bruno Toffolo
  • 1,504
  • 19
  • 24