Jump to content

Count % In The Submission Form


Recommended Posts

Hi Caspians. 

 

I need some help with a script. I have a Submission Form with 3 fields that will count evaluation of service in percents. I have created a virtual Field that says how many percents left, since Sum of 3 Field should be equal 100% and if Sum is bigger than 100% I want to show warning message. 

 

Is it possible ?

 

Any help will be appreciated. 

 

Michelle. 

Link to comment
Share on other sites

Hello Michelle. 

 

I think this script might help you: 

<SCRIPT LANGUAGE="JavaScript">

 function calculate()
 {
   var  field1 = parseFloat(document.getElementById("InsertRecordYour_Field1").value);
   var  field2 = parseFloat(document.getElementById("InsertRecordYour_Field2").value);
   var  field3  = parseFloat(document.getElementById("InsertRecordYour_Field3").value);
   var virtual=  field1+field2+ field3;
   var sum=100-virtual;


document.getElementById("cbParamVirtual1").value=sum;

if(document.getElementById("cbParamVirtual1").value<0)

{
alert(" SUM SHOULD NOT BE MORE THAN 100%");

}

 }



 document.getElementById("caspioform").onchange=calculate;
 document.getElementById("caspioform").onsubmit=calculate;


</SCRIPT>

I have created 1 Virtual Text Field and also have set "To receive Parameters " Default Value"=0"" for all three fields. 

 

 

I hope it helps.

 

Aurora :)  

Link to comment
Share on other sites

Hi AR,

 

Welcome to the forum!

How are you? How's the weather in your city?

 

I have edited the code, I hope, it works:

<SCRIPT LANGUAGE="JavaScript">
var fieldname1 = "Your_Field1";
var fieldname2 = "Your_Field2";
var fieldname3 = "Your_Field3";

fieldname1 = "InsertRecord" + fieldname1;
fieldname2 = "InsertRecord" + fieldname2;
fieldname3 = "InsertRecord" + fieldname3;

document.getElementById(fieldname1).value = 0;
document.getElementById(fieldname2).value = 0;
document.getElementById(fieldname3).value = 0;

function calculate()
 {
   var field1 = parseFloat(document.getElementById(fieldname1).value);
   var field2 = parseFloat(document.getElementById(fieldname2).value);
   var field3  = parseFloat(document.getElementById(fieldname3).value);
   var virtual = field1 + field2 + field3;
   var sum = 100 - virtual;
   document.getElementById("cbParamVirtual1").value=sum;
 }

function check()
{
 if(parseFloat(document.getElementById("cbParamVirtual1").value)!=0)
   {
    alert(" SUM SHOULD BE 100%");
    return false;
   }
}

document.getElementById("caspioform").onchange=calculate;
document.getElementById("caspioform").onsubmit=check;
</SCRIPT>

Don't forget to change names Your_Field1 to the names of your fields.

 

I'll be grateful, if you tell me if the code works.

Have a nice day!

Link to comment
Share on other sites

  • 4 months later...

Hi Xiang, 

 

This has worked great! But I've just realized that an additional condition needs to be included in here, and I'm not sure how to structure the language. I have a field in my Submission page called student_status, and I only want to apply the function check () and return false when student_status !== "I do not currently teach English but plan to when I finish my studies". If student_status == "I do not currently teach English but plan to when I finish my studies", I would like the user to be able to submit the page regardless of the sum value, as the calculate function does not apply to these users.

 

Would you have a suggestion on how to incorporate this additional condition into the script?

 

Thanks, 

 

Aimee

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...