Verify total of numeric entry fields
Douglas Dorfman created the topic: Verify total of numeric entry fields
I need to create a form allowing entry of 7 numeric fields.
When Submit is pressed, the total of the 7 fields must equal 100 for the entry to be valid, otherwise they get an error message.
I'd want to display a running total of the columns as they make these 7 numeric entries.
Each numeric field is a whole number between 0 and 100.
A copy of the paper form I need to automate is attached.
Thanks for your help!
Please Log in or Create an account to join the conversation.
- Topic Author
- Douglas Dorfman
- Offline
- Junior Boarder
8 years 8 months ago
Support replied the topic: Verify total of numeric entry fields
Dear Douglas,
You can do this by writing additional script to your form in Form Options > Javascript. The code should take the values of your fields and do the following:
- add all of them and get the sum,
- check if they sum is exactly 100,
- execute scripts for all of the statements.
This should be done with if/else if/else of jQuery, which is the easiest way to go. For example, lets say you have 3 fields:
var result = parseInt(jQuery('#field_id1').val()) + parseInt(jQuery('#field_id2').val()) + parseInt(jQuery('#field_id3').val());
if (result == 100) {
alert('Success!');
} else {
alert('The result is invalid.');
}
The code shoulde be written in before_submit function. Please make sure to pick the correct IDs of fields and let us know in case you have questions.
Thank you.
Best regards,
Web-Dorado team
Joomla extensions ,
Wordpress plugins ,
Drupal modules
Please Log in or Create an account to join the conversation.
8 years 8 months ago
Douglas Dorfman replied the topic: Verify total of numeric entry fields
Almost working...
If the column = 100 it works fine.
If it doesn't, it gives the error message. After you click OK, it refreshes the form and reports that it was entered successfully (instead of just leaving the screen as is).
Here's the javascript I'm using:
function before_submit() {
var result = parseInt(jQuery('#wdform_1_element13').val()) + parseInt(jQuery('#wdform_2_element13').val()) + parseInt(jQuery('#wdform_3_element13').val());
if (result != 100) {
alert('Total does not equal 100%.');
}
Also, I'd like to display a running total for the column on the form so the user can see the total of their entries.
Please Log in or Create an account to join the conversation.
- Topic Author
- Douglas Dorfman
- Offline
- Junior Boarder
8 years 8 months ago
Support replied the topic: Verify total of numeric entry fields
Dear Douglas,
If you want to use the script to validate inputs, please add the code to FMViewForm_maker.php file. Search for check_required<?php echo $form_id ?> function and find the following code:
if (window.before_submit) {
before_submit();
}
Write the code after this and also add return; after your alert() in if statement.
Thank you.
Best regards,
Web-Dorado team
Joomla extensions ,
Wordpress plugins ,
Drupal modules
Please Log in or Create an account to join the conversation.
8 years 8 months ago
Douglas Dorfman replied the topic: Verify total of numeric entry fields
All is fine. Thank you!
Please Log in or Create an account to join the conversation.
- Topic Author
- Douglas Dorfman
- Offline
- Junior Boarder
8 years 8 months ago
Douglas Dorfman replied the topic: Verify total of numeric entry fields
The fix we applied affects all forms, not just the one (Form ID 13). Below is the section of code from the FMViewForm_maker.php file where we applied the fix. What can we add so this function only affects form ID 13?
function check_required<?php echo $form_id ?>(but_type) {
if (but_type == 'reset') {
if (window.before_reset) {
before_reset();
}
window.location = "<?php echo $current_url ?>";
return;
}
if (window.before_submit) {
before_submit();
}
var result = parseInt(jQuery('#wdform_1_element13').val()) + parseInt(jQuery('#wdform_2_element13').val()) + parseInt(jQuery('#wdform_3_element13').val()) + parseInt(jQuery('#wdform_5_element13').val()) + parseInt(jQuery('#wdform_6_element13').val()) + parseInt(jQuery('#wdform_7_element13').val()) + parseInt(jQuery('#wdform_8_element13').val());
if (result != 100) {
alert('Total does not equal 100%.');
return;
}
Please Log in or Create an account to join the conversation.
- Topic Author
- Douglas Dorfman
- Offline
- Junior Boarder
8 years 8 months ago
Support replied the topic: Verify total of numeric entry fields
Dear Douglas,
Please use the following code instead, it detects your form with ID of 13:
if ( <?php echo $form_id ?> == 13 ) {
var result = parseInt(jQuery('#wdform_1_element13').val()) + parseInt(jQuery('#wdform_2_element13').val()) + parseInt(jQuery('#wdform_3_element13').val()) + parseInt(jQuery('#wdform_5_element13').val()) + parseInt(jQuery('#wdform_6_element13').val()) + parseInt(jQuery('#wdform_7_element13').val()) + parseInt(jQuery('#wdform_8_element13').val());
if (result != 100) {
alert('Total does not equal 100%.');
return;
}
}
Thank you.
Best regards,
Web-Dorado team
Joomla extensions ,
Wordpress plugins ,
Drupal modules
Please Log in or Create an account to join the conversation.
8 years 8 months ago
Douglas Dorfman replied the topic: Verify total of numeric entry fields
Thank you... works perfectly!
Please Log in or Create an account to join the conversation.
- Topic Author
- Douglas Dorfman
- Offline
- Junior Boarder
8 years 8 months ago