开发者

Looking for a jQuery text counter that can do multiple text areas! [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 8 years ago.

Improve this question

I've looked around and haven't able to find a plugin that will do exactly what I need:

  1. Be usable on multiple text areas per page.
  2. Have a minimum and maximum limit
  3. Display characters and warnings while the user types.

Ideally, it would be nice if the plugin/function prevented/enabled the form based on the character limit.

Where can I find something a plugin with such specs?

Should I write my own? I am a complete novice in jQuery and JavaScript, so the latter option is not desirable. But I c开发者_如何转开发an tinker and copy code.


It's easy to write such a thing...

$('textarea').keyup(function(){
    var maxsize = 1000;
    if( $(this).val().length > maxsize)
        $(element).val($(element).val().substring(0, maxsize));
});

You can also make it more generic... Just a basic example:

<textarea data-min="10" data-max="1000"></textarea>

$('textarea').keyup(function(){
    var maxsize = $(this).data('max');
    if( $(this).val().length > maxsize)
        $(element).val($(element).val().substring(0, maxsize));
});

$('textarea').blur(function(){
    var minsize = $(this).data('min');
    if( $(this).val().length < minsize)
        alert("Mininum size for this textarea is " + minsize);
});


I would write you own, based on the keypress event. Should be pretty simple:

var textarea = document.getElementById('someelement');
var formSubmitButton = document.getElementById('someelementsParentform');

textarea.addEventListener('keypress', function(){
    checklength(this, minvalue, maxvalue, formSubmitButton)
}, false);

function checkLength(thiselement, min, max, formSubmitButton){
if(thiselement.length < min || thiselement > max){
    formSubmitButton.disabled = true;
}else{
    formSubmitButton.disabled = false;
}

That's untested code, but i believe it should work

To use it on another textarea, just add an event listener to that text area and call the function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜