This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 2

Limit field characters


  1. Calan
    Member

    Pleas explain how I would limit the number of characters allowed in a field. I need to limit certain fields to say 6 or 8 numbers.

    Thanks.

    Posted 15 years ago #
  2. redityo

    Hi,

    To set the limit you need to edit "includes/view-functions.php", for example you want to limit "Text Field" with element id = 1 in form 1, then go around line 40 ~ 50, you will see this code :

    $element_markup = <<<EOT
    		<li id="li_{$element->id}" {$error_class}>
    		<label class="description" for="element_{$element->id}">{$element->title} {$span_required}</label>
    		<div>
    			<input id="element_{$element->id}" name="element_{$element->id}"
    class="element text {$element->size}" type="text"
     value="{$element->default_value}" />
    		</div>{$guidelines} {$error_message}
    		</li>
    EOT;

    replace the code with this one

    if ($_GET['id'] == 1 && $element->id == 1)
    			$set_max = 'maxlength="8"';
    		else
    			$set_max = '';
    
    $element_markup = <<<EOT
    		<li id="li_{$element->id}" {$error_class}>
    		<label class="description"
    for="element_{$element->id}">{$element->title} {$span_required}</label>
    		<div>
    			<input {$set_max}  id="element_{$element->id}" name="element_{$element->id}"
    class="element text {$element->size}" type="text"
     value="{$element->default_value}" />
    		</div>{$guidelines} {$error_message}
    		</li>
    EOT;

    those code will limit text field size to 8


    MachForm Support

    Posted 15 years ago #
  3. Calan
    Member

    Excellent. Thanks Redityo.

    Posted 15 years ago #
  4. kmac
    Member

    Hi,

    I've used the above code to set the character limit to 50 for a text area but it still allows the input of more characters.

    Also, I have a number of text areas in my form that must be limited to 50 characters. Is there a way that I could have a default set for all text areas in form_1 for instance?

    Many thanks

    Posted 15 years ago #
  5. redityo

    Hi,

    Have you change the form id and element id with yours in these code ?

    if ($_GET['id'] == 1 && $element->id == 1)
    			$set_max = 'maxlength="8"';
    		else
    			$set_max = '';

    Anyway if you need to apply the code in all text field on form 2 for example, you can change those condition to be like this :

    if ($_GET['id'] == 2)
    			$set_max = 'maxlength="8"';
    		else
    			$set_max = '';

    MachForm Support

    Posted 15 years ago #
  6. nunya
    Member

    Here's a question: I am integrating my form (id = 4) using the Advanced Code method (which does a php include), so the above code does not work because it is trying to $_GET the form ID and element ID from the url. My question is how can I limit the characters in a field when using the Advanced Code Form method?

    My form id is 4. I would like to limit element_id 9 and 10 to 4 characters and element_id 19 to 16 characters.

    Thank you!!

    Posted 15 years ago #
  7. redityo

    I see... advance embed code have different approach indeed. I think the easy way by editing "includes/post-functions.php" file to set a maximum input validation, so when the user input exceed maximum length, it'll show the error. To do so, go to around line 165 and you will see this code :

    $rules[$element_name]['max'] 		= 255;

    put these code exactly bellow that line

    if ($form_id == 4 )
    {
    	if ($element_id == 9 || $element_id == 10)
    		$rules[$element_name]['max'] 		= 4;
    	elseif ($element_id == 19)
    		$rules[$element_name]['max'] 		= 16;
    
    }

    Otherwise you can change my previous code, by adding a session variable in your PHP page. Add these code to your PHP page after "<?php"

    @session_start();
    $_SESSION['sess_form_id'] = 12;

    then change my previous code from :

    if ($_GET['id'] == 2)
    	$set_max = 'maxlength="8"';
    else
    	$set_max = '';

    to

    if ($_SESSION['sess_form_id'] == 2)
    	$set_max = 'maxlength="8"';
    else
    	$set_max = '';

    MachForm Support

    Posted 15 years ago #
  8. kdy
    Member

    Hi there,

    I am having som problems with this one.
    It seems that this solution is only working for "text" elements, but not "textareas"?
    I have a template ID 25 with element ID 9 where I need to limit characters to 160.

    I have tried replacing the "$element_markup" code with the one supplied by you.
    It works fine on element id 1 in my form, which is a text element. But when I change to element 9, which is a textarea, nothing happens?

    Could you please help me?

    Posted 14 years ago #
  9. Marconi
    Member

    How do I do this for multiple fields, but not all of the same type? For example, I have some number fields that I want to restrict to 2 numbers, others I want to restrict to 3.

    Posted 14 years ago #

RSS feed for this topic

Reply