Custom field validation in form

Hi,

I'm using some custom code in a boonex page to display a form.

 

Just to give you a bit of context my code creates inputs like this:

 

            $aInput = array(
                'type' => $sInputType,
                'caption' => $sFieldTranslation,
                'name' => "field_$sFieldName",
                'attrs' => array( 'id' => "Input_$sFieldName")
            );

            ...

 

 

Then calls this method to generate the form:

 

$oForm = new BxTemplFormView($aForm);

 

If I want to make a field required I add "required" to the class:

 

'attrs' => array('class' => 'required', 'id' => 'ReviewTitle')

 

My question is what should I add to have a validation on the length? I would like one field to be exactly 10 characters long.

 

I see from jquery documentation that there is a https://jqueryvalidation.org/maxlength-method/

 

But where should I add this into my code?

 

Thanks,

 Luca.

Quote · 6 Sep 2016

You may add it on footer.html or somewhere on the page you need. Don't forget to hook it to submit button or any other event.

if($("#ReviewTitle").val().length!=10) alert("Title should be 10 characters long.");
http://boonexpert.com
Quote · 7 Sep 2016

I think that the field should be modified the following way:

            $aInput = array(
                'type' => $sInputType,
                'caption' => $sFieldTranslation,
                'name' => "field_$sFieldName",
                'attrs' => array( 'id' => "Input_$sFieldName"),
                'required' => true,
                'checker' => array (
                    'func' => 'length', 
                    'params' => array(0,10),
                    'error' => 'length must be less than 10 characters',
                ),
            );
Rules → http://www.boonex.com/terms
Quote · 10 Sep 2016

Hi and many thanks for your answers!

 

I tested the solution of boonexpert and works well. I get the pop up. The only change I did is to return false in the form validation, so:

 

if($("#ReviewTitle").val().length!=10) { alert("Title should be 10 characters long."); return false; }

 

I tested also the solution of AlexT but it doesn't work. I have boonex version 7.1.4. I will then use the javascript solution.

 

 

Quote · 11 Sep 2016
 
 
Below is the legacy version of the Boonex site, maintained for Dolphin.Pro 7.x support.
The new Dolphin solution is powered by UNA Community Management System.