Jump to content
  • Advertisement
Sign in to follow this  

[web] Anyway to set an input textfield's maxlength using javascript?

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I was wondering if its possible to set an input textfield's maxlength with javascript? I've tried document.getElementById("abc").setAttribute("maxlength", 3), but this only works for things like size, value etc, not maxlength for some reason. Any ideas? Thanks

Share this post

Link to post
Share on other sites
Found this one somewhere:

<head><script language="JavaScript">
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring([0], maxlimit);
countfield.value = maxlimit - field.value.length;


<form name=myform action="">
<font size="[1]" face="arial, helvetica, sans-serif"> ( You may enter up to [125] characters. )<br>
<textarea name=message wrap=physical cols=[28] rows=[4] onKeyDown="textCounter(this.form.message,this.form.remLen,[125]);" onKeyUp="textCounter(this.form.message,this.form.remLen,[125]);"></textarea>
<input readonly type=text name=remLen size=[3] maxlength=[3] value="[125]"> characters left</font>


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!