SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    Resistance is Futile webgodjj's Avatar
    Join Date
    Nov 2002
    Location
    Madison, WI USA
    Posts
    448
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jquery count two text fields

    Hi,

    I have seen many character counter scripts (with feedback), to count the characters of a text box. However, I am trying to accomplish the same thing however need to make sure the combined value of two text boxes don't exceed a limit. Ie... there is a visual countdown of number of characters remaining (val) of the two text boxes combined, and an error pops up if it is exceeded.
    Atomic Coffee Media
    http://www.atomiccoffee.com
    JJ Pagac

  2. #2
    SitePoint Enthusiast ironikart's Avatar
    Join Date
    Apr 2005
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could do something neat with a jQuery plugin:

    Code:
    jQuery.fn.getLength = function(){
       var l = 0;
       this.each(function(){
           l += jQuery(this).val().length;
       });
       return l;
    };
    
    var numChars = jQuery('input').getLength();
    if (numChars > whatever) {
    // error
    }
    Untested, and I think it would blow up nicely if you put anything in your selector that didn't support .val(), but serves the purpose. You could also show visually the combined character count:

    Code:
    var $inputs = jQuery('#input1,#input2');
    $inputs.bind('keyup',function(){
         jQuery('#yourcounthere').html($inputs.getLength());
    });


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •