Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3431718
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:21:39+00:00 2026-05-18T07:21:39+00:00

I have a function that displays a countdown next to a text field for

  • 0

I have a function that displays a countdown next to a text field for the number of characters in the field (think twitter)

<script language="javascript" type="text/javascript">
function countDown(control, maxLen, counter, typeName) {
     var len = control.value.length;
     var txt = control.value;
     var span = document.getElementById(counter);
     span.style.display = '';
     span.innerHTML = (maxLen - len);
     if (len >= (maxLen - 10)) {
          span.style.color = 'red';
     } else {
          span.style.color = '';
     }
}
</script>

And the next field down takes a comma separated value. Example:

tomato, apple, orange, pear

and I’d like to limit that list to 5 things (and 4 separating commas).

How can I make a similar function that counts down for the number of commas in the input.

I got this started, but it’s not changing the value in the span.
my Javascript

<script language="javascript" type="text/javascript">
    var max = 5;

    function commaDown(area,ticker){

    // our text in the textarea element
    var txt = area.val();

    // how many commas we have?
    var commas = txt.split(",").length;

    var span = document.getElementById(ticker);

    //var commas ++;
    if(commas > max) {
      // grab last comma position
      var lastComma = txt.lastIndexOf(",");
      // delete all after last comma position
      area.val(txt.substring(0, lastComma));
      //it was count with + 1, so let's take that down
      commas--;
    }
    if (txt == '') {
      commas = 0;
    }
    // show message
    span.innerHTML = (max-commas);
  }
</script>

and my html (I think the problem lies here)

<input id="choices" type="text" name="choices" class="text medium" onkeyup="commaDown('choices','limit');"/> <span id="limit">5</span><br/>

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:21:40+00:00Added an answer on May 18, 2026 at 7:21 am

    Something like this (assuming you have a text field with id csv)

    document.getElementById('csv').onkeydown = function(e){
        if (!e) var e = window.event;
        var list = this.value.split(',');
         if (list.length == 5 && e.keyCode  ==  '188' )
         {
             // what to do if more than 5 commas(,) are entered
             // i put a red border and make it go after 1 second
             this.style.borderColor ='red';
             var _this  =  this;
             setTimeout(function(){
                  _this.style.borderColor='';
                  _this.disabled=false;
             },1000);
             // return false to forbid the surplus comma to be entered in the field
             return false;
         }
    }
    

    example at http://www.jsfiddle.net/gaby/YEHXf/2/


    Updated Answer

    You seem to have mixed parts of jQuery in your code and that causes the script to fail

        var max = 5;
    
        function commaDown(_area, _ticker){
        var area = document.getElementById(_area);
    
        // our text in the textarea element
        var txt = area.value;
    
        // how many commas we have?
        var commas = txt.split(",").length;
    
        var span = document.getElementById(_ticker);
    
        //var commas ++;
        if(commas > max) {
          // grab last comma position
          var lastComma = txt.lastIndexOf(",");
          // delete all after last comma position
          area.value = txt.substring(0, lastComma);
          //it was count with + 1, so let's take that down
          commas--;
        }
        if (txt == '') {
          commas = 0;
        }
        // show message
        span.innerHTML = (max-commas);
      }
    

    live example at http://jsfiddle.net/z4KRd/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that displays comments in DIV box from a database. The
In my page I have a carousel that displays short descriptions of products on
I have a jQuery tooltip plugin that displays a tooltip when I hover over
I have a display object that displays a list of Users and provides a
I have a web app that displays a web page in a web view.
I have a Module in VB.NET called Math.vb and in that module, I created
I have a flex app that used to be an internet based app. In
I'm trying to make a modal view which displays the champion of my app.
This samplet is from a project I am working on. My client uses software
If you goto the netflix site and hover over a movie http://www.netflix.com/BrowseSelection You get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.