client side check of multiple fields

emmexx 10 Nov, 2012
I have a form where there are 2 fields and at least one should be non empty (it's just a test case, the real form has more fields to check).
I know I can do some check on onsubmit but how do I manage error messages? Is there a standard way to do that?

Thank you
maxx
emmexx 11 Nov, 2012
I found an answer on another thread but it doesn't work. 😟

I followed this example on the formcheck forum. On that discussion the poster suggested to modify the formcheck code but I didn't do that.

I added a validate['%almostOne'] class to 2 fields and added a function almostOne to a loadJS action.
When I submit the function executes but no tip appears near the 1st field, on the other hand a tip appears next to the 1st required field.

The error message appears only if the fields are mandatory (Required). But obviously I don't need 2 required fields...

What can be wrong with my setup? Should I modify formcheck code used by CF?
I don't want to do that because it's a trick, if i need to check 2 other fields I'd have to change again the formcheck code. :?

thank you
maxx
Max_admin 12 Nov, 2012
Hi,

What do you have in your function code ? this is the source of the problem, give a unique id to your fields, e.g: field1, field2 then use this in the function:

function almostOne(el){
if($('field1').get('value') == '' && $('field2').get('value') == ''){
$('field1').errors.push("Please enter something here or there");
$('field2').errors.push("Please enter something here or there");
return false;
}
}


Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
emmexx 12 Nov, 2012

What do you have in your function code ? this is the source of the problem, give a unique id to your fields, e.g: field1, field2 then use this in the function:



Thank you Max,

I used a function similar to the one you suggested. I copied it from the formcheck forum.
The only difference is:

el.errors.push...
instead of
$('field1').errors.push...

It doesn't work both ways.

The function is working. I mean the push function is executed and the function returns false.
In firebug I can see that something inside the formcheck file is executed (the code is compressed, I can't debug it).

I should try to debug with an uncompressed version of formcheck.

Other ideas?

Thank you
maxx
emmexx 12 Nov, 2012

I should try to debug with an uncompressed version of formcheck.



The problem is in the ManageError function of formcheck.

 if ((!isValid && el.validation.contains('required')) || (el.value && !isValid)) {
...
 } else if ((isValid || (!el.validation.contains('required') && !el.value))) {
   this.removeError(el);
   return true;
} 


In my case:
[list]
  • isValid is false

  • el.validation.contains('required') is false

  • el.value is false (blank)
  • [/list]

    So the 1st if evaluates to false, the 2nd if is true and removes the error.

    I'll see if I can use a trick to let the 1st if run.

    maxx
    emmexx 12 Nov, 2012

    I'll see if I can use a trick to let the 1st if run.



    I found a not very elegant solution that should work for most of my users.

    I added a dot as a default value for the 2 fields.
    In the almostOne function I check for the dot value:

    if ($('field1').value=='.' && $('field2').value=='.')
    ...


    On load I added a focus and a blur event to field1 and field2. On focus I clear the dot and on blur I add a dot if the field is empty.

    bye
    maxx
    This topic is locked and no more replies can be posted.