user2783853
user2783853

Reputation: 137

textarea maxlength doesnot work on enter key press

I have a textarea which mas its maxlength set to 200 and it works. But if the user while enter the characters, hits enter and enters 199 characters of hits enter twice and then enters 198 chars, the textarea displays the message that max 200 characters should be entered.

This problem persists in IE but not in chrome

Upvotes: 2

Views: 1133

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34846

The issue you are seeing is the difference in how Chrome and IE treat the enter key.

  • Chrome treats the enter key as \r\n (two characters)
  • IE treats the enter key as just \n (one character)

This is illustrated by this jsFiddle.


Results:

  • Chrome

    1\r\n
    2\r\n
    3\r\n
    4\r\n
    5\r\n
    6\r\n
    78
    

Note: You cannot press enter after the 7, because it will count as 2 characters and exceed the max of 20, because the number 7 represents the 19th character.

  • IE

    1\n
    2\n
    3\n
    4\n
    5\n
    6\n
    7\n
    8\n
    9\n
    10
    

Interestingly, jQuery treats the enter key as a single character too (\n), thus if you use jQuery to count for you, then you will end up with the same "problem" that IE has.

Upvotes: 2

Related Questions