LITguy
LITguy

Reputation: 623

Pasting instead of typing text into input field with character limit javascript

I have code that limits number of characters with a sort of "countdown" field that works in real-time as the user types.

Everything works perfectly except when the user "pastes" their text. Only when they click in the field again does it reset and know there are too many characters entered.

Is there a way to make it work on mouseup / out... or interact with a paste by user?

Here is the current code:

<script type="text/javascript">
function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit)
field.value = field.value.substring(0, maxlimit);
else
cntfield.value = maxlimit - field.value.length;
}
</script>

Upvotes: 0

Views: 1060

Answers (1)

Evan Hahn
Evan Hahn

Reputation: 12712

Try the maxlength property:

<input type="text" maxlength="10">

Upvotes: 1

Related Questions