vendredi 22 juin 2018

Decrement the counter in javascript when hyperlink is typed in a TextArea

I have a textArea in which the Text will be typed and when text is typed the counter will be decreased by 1 for each character typed.. but I want when the landing link is typed in the textArea the counter will be decrement by exactly 16 and after that when normal text is typed again it will be decremented by 1 for each character .... any kind of help will be appreciated thanks

TextArea

<div class="col-sm-offset-2 col-sm-10">
            <label>Message</label>
               <textarea id="msgArea" class="form-control" rows="3"></textarea>
               <span id="spnCharLeft"></span>
            </div>

JavaScript Code

 var maxLimit = 160;
         $(document).ready(function(e) {
            $('#msgArea').keyup(function(){

                 var lengthCount = this.value.length;              
            if(lengthCount > maxLimit) {
 this.value = this.value.substring(0, maxLimit);
                var charactersLeft = maxLimit - lengthCount + 1; 

                    $('#msgArea').css('background-color','#FFFF00');


            }
            else { 
            var charactersLeft ;
            var textmsg = document.getElementById('msgArea').value;
            var pattern = new RegExp("((http|https)(:\/\/))?([a-zA-Z0-9]+[.]{1}){2}[a-zA-z0-9]+(\/{1}[a-zA-Z0-9]+)*\/?", "i");
if(pattern.test(textmsg)){
            lengthCount = lengthCount - 16;
            charactersLeft = maxLimit - lengthCount;
        }
        else {

                 charactersLeft = maxLimit - lengthCount;  
                  $('#spnCharLeft').text(charactersLeft + ' Characters left');

            }
            }
            $('#spnCharLeft').css('display', 'block');

            if(lengthCount<maxLimit){
            $('#msgArea').css('background-color','#fff');
            }
        });




Aucun commentaire:

Enregistrer un commentaire