Jump to content
Fórum Script Brasil
  • 0

Contar dígitos num textbox de múltiplas linhas ignorando espaços


Question

Olá pessoal,

Fiz o código abaixo para contar os caracteres digitados num textbox de múltiplas linhas mas ele conta os espaços. Preciso que, na contagem, ignore os espaços tanto antes e depois como entre as palavras.

<script language="javascript" type="text/javascript">

        function countChars(countfrom, displayto) {
            
            var len = document.getElementById(countfrom).value.replace(/^\s+|\s+$/g,'').length;
            document.getElementById(displayto).innerHTML = "Nº de Dígitos: " + len;
            
            if (len == 0)
            {
                document.getElementById(displayto).innerHTML = "";
                document.getElementById('divContar').className = 'contar1';
            } else
                if (len > 0 && len < 1000)
                {
                    document.getElementById('divContar').className = 'contar1';
                } else 
             
                if (len > 0 && len <= 1000)
                {
                    document.getElementById('divContar').className = 'contar2';
                } else 
                    if(len > 1000 && len <=1500)
                    {
                        document.getElementById('divContar').className = 'contar2';
                    } else
                        if(len > 1500)
                        {
                            document.getElementById('divContar').className = 'contar3';
                        };
        }
    </script>

 

Já alterei a linha 

var len = document.getElementById(countfrom).value.replace(/^\s+|\s+$/g,'').length;

para

var len = document.getElementById(countfrom).value.trim().length;
var len = document.getElementById(countfrom).value.replace(/^\s+|\s+$/g,'').trim().length;

 

E não funcionou.

Alguém pode me ajudar?

Grato,

 

ilano.

Edited by ilano
Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      148738
    • Total Posts
      644613
×
×
  • Create New...