Limiting the number of characters in a text field

I’m trying to add a maxlength attribute to a dynamic form but no matter what I do it doesn’t work. This is what I’m using:


and this is the whole form:

  $FORM[$formid]['field']['title']['label']="What is your favourite book?";

Is it being applied to the form input as maxlength=“40”?

<input type="text" name="title" maxlength="40" />

This should work.

Unfortunately no I thought that would work but this is I’m getting this instead:

<input id="title" type="text" name="title">

If you want to validate before the form is submitted you will need to do this client side either using html5 validation: or by submitting the data via Ajax and then checking against that

if (strlen($value) > 40){
return false;

Did you write the form rendering code or are you using some pre-made program?
Yes, form processing can limit the data being stored as well.

Thanks, I’ve already got that bit sorted - the problem is that people are entering a lot of text them getting frustrated when they’re told it’s too many characters

I’ve started in a new job and they want me to limit the number of characters that are allowed in the box so at the moment I’m not allowed to change the way the form is created - although I would before to!

Well if you are not allowed to edit the input field I’m not sure what can be done.

If you can’t edit the form then you will have to bind an event to onsubmit (javascript), prevent the default, check the length and then submit it if it checks out (or flash an error if it doesn’t).

Sorry that wasn’t very clear. I can edit the form/code but I’m not allowed to change it entirely so I have to adapt the code rather than rewrite it completely.

I think Drummin’s question about whether you wrote the code that renders the form was driving at whether the code that does the rendering actually supports you specifying the maxlength parameter - if it doesn’t, and you can’t change it, that’s going to make it impossible, surely?

In the first part of the first post you wrote that you’re using:


but then a bit later you said you used:


Should that [‘err’] section be there, or am I misreading the post?

Oh I see what you’re staying, I didn’t realise that could be the case.

I tried both of them and neither worked so I thought it was a better to post them both - sorry for the confusion

I guess anything’s possible - but it’s a bit strange given that maxlength is hardly a new idea by any means, you’d kind of expect that a form-rendering function might support something brand new and up to the minute (if there is anything like that in html forms) but not something that’s been around since the beginning of time.

So, do you have access to what draws the form? Or is it a third-party package you can name that might have some esoteric method of setting the length?

I’ve just found the function that displays the form and I’m guessing this part might be controlling it:

    # input start
    echo "<div id=\"input-".$formid."-".$key."\" class=\"input_input\" style=\"margin: 0 5px;\""; if( $value['inset'] ) { echo " style=\"min-width: ".$value['width']."px;\""; } echo ">\n";

    # text only
    if( $value['type']=="textonly" ) { echo $value['text']."<br />"; }
    # text only title
    if( $value['type']=="textonlytitle" ) { echo "<br /><h4 style=\"font-size: 30px;\">".$value['text']."</h4>"; }

    # text input
    if( $value['type']=="text" ) {
      echo "<input id=\"".$formid."-".$key."\" class=\"input_text".$autofocus."\" style=\"width: ".$value['width']."px;\" type=\"text\" name=\"".$key."\" value=\"".htmldisplay( $GLOBALS['current'][0][$key] )."\" />\n";

I’m not sure what to change though

Hard to say for sure not knowing how you get from




Well however you get to the KEY=VALUE pair you could do something like this.

$maxlength = (!empty($value['title']['maxlength']) ? ' maxlength="' . $value['title']['maxlength'] . '"' : '');

echo '<input id="title" type="text" name="title"' . $maxlength . '>';

I changed it to this but it’s not showing maxlength for any text fields at all.

echo "<input id=\"".$formid."-".$key."\" class=\"input_text".$autofocus."\" style=\"width: ".$value['width']."px;\" type=\"text\" name=\"".$key."\" maxlength=\"".htmldisplay( $value['title']['maxlength'] )."\" value=\"".htmldisplay( $GLOBALS['current'][0][$key] )."\" />\n";

Maybe showing current $FORM array(POST 1) you are using, How the building function is called and the FULL form building function so we can see how $value is defined might help.

There’s too much code to paste it here so I’ve attached the function code as well as the form code. The form itself is rendered on the page using this:

<a class="attachment" href="/community/uploads/default/24903/0e2b533887cb797c.php">form.php</a> (4.2 KB) <a class="attachment" href="/community/uploads/default/24904/d18cd7fd4aa80433.php">function.php</a> (25.2 KB) <?php form_show( 0 ); ?>

Be consistent in defining KEYS. If you change rendering key to max instead of max2 you should get the value defined.

              echo "<input id=\"".$formid."-".$key."\" class=\"input_text".$autofocus."\" style=\"width: ".$value['width']."px;\" type=\"text\" name=\"".$key."\" maxlength=\"".htmldisplay( $value['err']['max'] )."\" value=\"".htmldisplay( $GLOBALS['current'][0][$key] )."\" />\n";