How to insert a js function in a field form?

kiwiup 20 Mar, 2015
Hi,
I have to insert a js function to a field form like this
<tr><td>sunday:</td><td><input type="text" name="sunday" size="4" id="sunday" onblur="checkmin()"></td></tr>

The question is:
in the designer tab I inserted a text field named sunday. Now I have insert a js function custom connected with this field. I found out a place in Validation tab called Custom function.
I wrote checkmin() inside but hoiw can specify onblur?

Thanks
GreyHead 20 Mar, 2015
Hi kiwiup,

I would do it with this code in a Load JS action:
jQuery(document).ready(function(jQ){
  jQ('#Sunday').blur(checkmin);
});

Are you sure that onBlur is the best event for this?

Bob
kiwiup 20 Mar, 2015
Hi Bob,
my task is to achieve a check on sum of numeric fields before submit a form.
Example:
field weekhour = 30
field mondayhour=2
...
field saturdayhour=2
field sundayhour=6

If (weekhour != mondayhour + tuesdayhour +......+ sundayhour)
then waring message

sundayhour is the last field inserted so when I leave it start js validation.

I will try your hint.

Thanks

Kiwiup
GreyHead 20 Mar, 2015
Hi Kiwiup,

Why not just make the weekhour readonly and calculate the total as the user enters the day. Or, if you want it as a check have a second weekhour_check and compare the two. I think you could then use the built-in Confirm validation check to make sure that they are the same.

Bob
kiwiup 20 Mar, 2015
Hi Bob,
the user inserts all values also weekhour.
I made the js function to get this check and it works. I would like to know how can properly insert this function on CF5.
I have 8 fields from weekhour to sundayhour.
How can get the sym of hour from monday to sunday, save this sun in a hidden field called weekhour_check to compare with weekhour?

Thanks.

Kiwiup.
GreyHead 23 Mar, 2015
Hi KiwiUp,

You'll have a JavaScript function something like this:
jQuery(document).ready(function(jQ){
  jQ('#Monday').keyup(checkmin);
  jQ('#Sunday').keyup(checkmin);

  function checkmin() {
   var mon., tue, wed, . . .sun. weekehour ;
   mon = parseInt(jQ('#Monday'),val());
  . . . 
   sun = parseInt(jQ('#Sunday'),val());
   total = parseInt(mon) + . . . + parseInt(sun);
   
    jQ('#weekhour_check').val(total);
    // something here to compare the two ??
    weekhour = jQ('#weekhour_check').val();
     if ( total != parseInt(weekhour) ) {
       // do something
     }
  }
});

Bob
kiwiup 23 Mar, 2015
Hi Bob,
later I will check your question.
Thanks
kiwiup 23 Mar, 2015
Hi Bob,
I made a form with 8 fields:
1 for monday
2 for tuesday
3 for wednesday
4 for thursday
5 for friday
6 for saturday
7 for sunday
8 for weekhour
I inserted a button to check if the sum from mon to sun is equal to weekhour.

Can you help me?
Thanks

Kiwiup


jQuery(document).ready(function(jQ){
  jQ('#Monday').keyup(checkmin);
  jQ('#Tuesday').keyup(checkmin);  
  jQ('#Wednesday').keyup(checkmin);
  jQ('#Thursday').keyup(checkmin);
  jQ('#Friday').keyup(checkmin);
  jQ('#Saturday').keyup(checkmin);
  jQ('#Sunday').keyup(checkmin);

  function checkmin() {
   var mon., tue, wed,thu,fri,sat,sun,weekehour ;
   mon = parseInt(jQ('#Monday'),val());
   tue = parseInt(jQ('#Tuesday'),val());
   wed = parseInt(jQ('#Wednesday'),val());
   thu = parseInt(jQ('#Thursday'),val());
   fri = parseInt(jQ('#Friday'),val());
   sat = parseInt(jQ('#Saturday'),val());
   sun = parseInt(jQ('#Sunday'),val());    
total =  parseInt(mon) + parseInt(tue) +parseInt(wed) +parseInt(thu) + parseInt(fri)+ parseInt(sat)+ parseInt(sun);
   
    jQ('#weekhour_check').val(total);
aler('Warning the total is different by weekehour ')
    weekhour = jQ('#weekhour_check').val();
     if ( total != parseInt(weekhour) ) {
       // do something
     }
  }
});
GreyHead 24 Mar, 2015
Hi Kiwiup,

What is the button for? - the code I sketched out is set up to run automatically every time one of the inputs changes.

What isn't working?

Bob
kiwiup 24 Mar, 2015
Hi Bob,
I inserted hte js code above in the On Load form before HTML Render form.
But after inserting a value in a field day (mon, tue ...sun) nothing happen.
Maybe I am making something wrong.
To resume:
I made a form with simple text felds (8)from mon to sun and weekhou).
I inserted a js code above in On Load before HTML Render form.
I will get that when I insert a value in a day starts a function checkmin to calculate the total againts weekhour showing a alert message if these fields are different.

I have to do something else?

Thanks
kiwiup 24 Mar, 2015
Hi Bob,
this is my form
http://www.fabiocenci.it/bd/index.php?option=com_chronoforms5&chronoform=exampleweekday

Thanks

Kiwiup
GreyHead 24 Mar, 2015
Hi KiwiUp,

Please remove the . I left in after 'mon'
var mon., tue, wed, . . .sun. weekehour ;
That's not needed and is causing a JS error. NB There may be other bugs in my code, please check it carefully.

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