Properly Validate TextArea Length (IE vs. Firefox)

2017/06/26
By
Modified: 2017/11/12

IE and Firefox Evaluate Length of a String Differently

Let’s consider this example, where HTML page has a TEXTAREA and an INBOX.  When user types inside TextArea, Inbox should show the length of TextArea string.

<script type="text/javascript"> 
 function TTLength(objTT){ 
 var tt= document.getElementById('TXT1'); 
 tt.value = objTT.value.length; 
 } 
</script> 
<textarea onKeyDown="TTLength(this);" onKeyUp="TTLength(this);" rows="10"> 
 This Text Area Contains a string With Newline Characters 
</textarea>
 <input type="text" id="TXT1" value="TT Length"/>

Let’s see how this page behaves in IE8 and in Firefox 3.6.11.

IE8 Firefox

As you can see that IE and FF return two different values for the length of an identical  string in TextArea and that could be a serious problem, if you are trying to validate the string length before updating the database.

It appears that IE counts all characters in a string, and Firefox ignores newline characters.  That explains the difference in the result.  Now, how do we fix that?

My only solution at this point is to add a server-side validation in addition to JavaScript to prevent ugly SQL errors on update.

Tags: , , , , , ,


6 Responses to Properly Validate TextArea Length (IE vs. Firefox)

  1. Pingback: URL
  2. placeoweb
    2012/04/16 at 08:31

    In my case i use (client side) solution of couting New Lines,
    http://blog.sarathonline.com/2009/09/javascript-maxlength-for-textarea-with.html

    (FRENCH) javascript textarea string .length diffère entre firefox, chrome et internet explorer
    http://dotclear.placeoweb.com/post/javascript-textarea-string.length-diff%C3%A8re-entre-firefox-chrome-et-internet-explorer

  3. CNA Training
    2010/12/17 at 14:26

    Useful blog website, keep me personally through searching it, I am seriously interested to find out another recommendation of it.

  4. Udar Gromov
    2010/12/15 at 23:43

    Прямая связь с редакцией: http://www.domainwebcenter.com/?page_id=449
    Меню:
    About –> To the Editor – Прямая Связь

    Direct message to the Editor: http://www.domainwebcenter.com/?page_id=449
    Use menu option
    About –> To the Editor – Прямая Связь
  5. Видео приколы
    2010/12/15 at 22:12

    Пишу тут раз обратной связи не нашла. У меня иногда пишет ошибку вот тут http://www.domainwebcenter.com Ошибка 403 «Доступ запрещен»
    Что мне делать ? я иногда статьи у вас беру для своей газеты.

  6. radiopodarok
    2010/12/10 at 07:47

    Спасибо! Буду теперь заходить на этот блог каждый день!Интересные посты – это ваш стиль безусловно!

Add Your Comment Ваш Комментарий

Your email address will not be published. Required fields are marked *

* Ваше Имя *
* Ваш Email (не будет показан на сайте)*

*