/    Sign up×
Community /Pin to ProfileBookmark

Character Limitation

Hello,

I’m using a script on a form that limits the number of charatcers used in a textarea field. I currently have set the textarea to a limit of 255 characters. I have noticed that when a user does a copy and paste of content that is more than 255 characters, the extra characters will be added to the textarea field. But if a user types in the conetent, it will max out at 255.

Is there a way to setup the script or the textarea so if a user types or does a copy and paste, the content will max at 255 only?

-Javascript-

<!– Begin
function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit) // if too long…trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update ‘characters left’ counter
else
cntfield.value = maxlimit – field.value.length;
}
// End –>

-Textarea Code-

<textarea name=”quick_bio” wrap=”virtual” cols=”51″ rows=”5″
onKeyDown=”textCounter(document.form.quick_bio,document.form.remLen1,255)”
onKeyUp=”textCounter(document.form.quick_bio,document.form.remLen1,255)”>
</textarea>

Thanks;

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@CrazyGazJun 25.2003 — the way I would do it is.

<script language="javascript">

function validate() {

if (document.form1.txtarea1.value.length > 255 ){

alert("you cannot exceed 255 characters")

return false;
}

}

</script>

<form action=***URL*** onSubmit="return validate();"

<textarea name="txtarea1"></textarea>

hope this helps, Gaz
Copy linkTweet thisAlerts:
@CharlesJun 25.2003 — [font=monospace]<textarea name="quick_bio" wrap="virtual" cols="51" rows="5"

onkeydown="textCounter(document.form.quick_bio,document.form.remLen1,255)"

onkeyup="textCounter(document.form.quick_bio,document.form.remLen1,255)"

onchange="textCounter(document.form.quick_bio,document.form.remLen1,255)">

</textarea> [/font]
×

Success!

Help @jordanj spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...