Sign in to follow this  

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this