Gather Maxlength value from form input

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
papa_smurf
Forum Newbie
Posts: 14
Joined: Tue Jun 01, 2010 12:22 pm

Gather Maxlength value from form input

Post by papa_smurf »

Hey all, quick question.

I'm writing a small validation script that has to check the maxlength of the form input and adjust the actual length of the form accordingly.

Here's what i'm talking about:

Code: Select all


function checkform() {
var elem = document.getElementById('r001').elements; 
for(var i = 0; i < elem.length; i++) { 

if(elem[i].type == "text") {
if(elem[i].value.length < elem[i].maxlength) {
//length is not long enough and will be adjusted accordingly
} else {
//length is long enough and will be passed through
}
}else if(elem[i].type == "radio") {
//ignore radio buttons
} else {
//ignore buttons
}
} 
return true;
} 

<form action="<?php echo $PHP_SELF;?>" method="post" id="r001" name="r001"  onsubmit="return checkform()">
<input name="CUST" type="text" size="40" maxlength="40" value="0" />
<input name="Author" type="text" size="20" maxlength="20" value="0" />
</form>



So what i need is something to replace the elem.maxlength in the code above, something that actually works. lol.

Any help would be greatly appreciated.
papa_smurf
Forum Newbie
Posts: 14
Joined: Tue Jun 01, 2010 12:22 pm

Re: Gather Maxlength value from form input

Post by papa_smurf »

hahahaha, I found the code.

For all that care, you can get the maxlength of a text input in a form by using:

Code: Select all

getAttribute("maxlength")
Post Reply