Chris_O
Chris_O

Reputation: 3444

jQuery Count limit input across multiple fields?

Is it possible to limit number of characters across multiple fields?

I'm using jQuery count and need to set a total character limit across multiple fields.

If limit is 100 and user enters 75 char in 1st field they should only be able to enter 25 characters in 2nd field.

DEMO: http://jsfiddle.net/2xfCG/

Example:

<form id="count">
<textarea id="one" class="text" ></textarea>
<textarea id="two" class="text" ></textarea>
<span id="left" />
</form>

<script>
$(function() {
$('#one, #two').limit('140','#left');
});
</script>

Above does not work. Also tried $('#one', '#two").limit('140', '#left');

Upvotes: 0

Views: 1087

Answers (1)

mu is too short
mu is too short

Reputation: 434685

Yes it is possible but not with the plugin you're using. All you need to do is bind a keyup handler to both textareas and then check the sum of the lengths of the values of both textareas:

var total_length = $('#textarea1').val().length + $('#textarea2').val().length;

If the total length exceeds what you want then truncate the text area that currently has focus.

Upvotes: 1

Related Questions