Michelle Posted November 3, 2015 Report Share Posted November 3, 2015 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. Quote Link to comment Share on other sites More sharing options...
Aurora Posted November 3, 2015 Report Share Posted November 3, 2015 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 Quote Link to comment Share on other sites More sharing options...
wfadmin Posted November 4, 2015 Report Share Posted November 4, 2015 Aurora, This is fantastic and exactly what I needed. Is there a way to also add in an alert if the value>=0? (i.e. if the sum is less than 100%)? I want to ensure that those responding to the form enter exactly 100 percent in the fields specified. Thanks. AR Quote Link to comment Share on other sites More sharing options...
Xiang Posted November 5, 2015 Report Share Posted November 5, 2015 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! Quote Link to comment Share on other sites More sharing options...
wfadmin Posted March 14, 2016 Report Share Posted March 14, 2016 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 Quote Link to comment Share on other sites More sharing options...
munkitkat Posted March 16, 2016 Report Share Posted March 16, 2016 This is fantastic and exactly what I needed. Is there a way to also add in an alert if the value>=0? (i.e. if the sum is less than 100%)? I want to ensure that those responding to the form enter exactly 100 percent in the fields specified. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.