Skip navigation
VickiTuch
Currently Being Moderated

Javascript alert script needed when calculation does not equal 100

Mar 5, 2012 10:48 AM

I have created a fillable form and have a total box which I've set to calulate the sum of Field1, Field2, Field3 and Field4

which works well.

 

I would like to add a javascript alert if the total amount does not add to exactly 100 to stop the user submitting the form or moving to next question.

 

Any help would be greatly appreciated

 

Thank you

 
Replies
  • Currently Being Moderated
    Mar 5, 2012 1:11 PM   in reply to VickiTuch

    I don't think it's a good idea to do this when the user fills in the

    fields. In fact, it will make the form unusable. Imagine that all fields

    are empty and the user fills in the first field with 20 (for example). They

    would then get this message, but of course there's nothing they can do

    about it, except for changing the value to 100... In short, not a good idea.

     

    If you want to add an error message to the submit button in case the sum is

    not 100, that's possible, but only if you submit the form using a script as

    well. The code to do that would be something like this:

     

    if (this.getField("Total").value==100) {

    this.submitForm(); // you'll need to specify the relevant parameters here...

    } else {

    app.alert("Error! The total must be equal to 100.");

     

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 5, 2012 1:34 PM   in reply to VickiTuch

    The code I provided will only submit if the fields Total equals 100. But

    you must not use the built-in "Submit a Form"-command for your button. Just

    a "Execute a JavaScript"-command with the provided code.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 5, 2012 1:53 PM   in reply to VickiTuch

    I'm not sure where you're placing this code... But you don't need to use

    getField. event.target already returns the field object you want to use.

     

    To show the yellow warning icon, change your alert command to this:

    app.alert("This field is required. Please enter your Name.",1)

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 6, 2012 6:43 AM   in reply to VickiTuch

    It seems like you've used some product to generate your code for the submit

    button. You will need to incorporate the line of code that checks if Total

    is 100 into that code.

     

    Yes, it looks OK, but next time you post a file, do so without the Reader

    Rights.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 6, 2012 6:44 AM   in reply to Gilad D (try67)

    Maybe I spoke too soon... The way you set it up now, the error message appears (when the fields do not equal 100) each time you change one of the other fields as well (like pressing one of the radio buttons), which is a bit odd.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 6, 2012 6:59 AM   in reply to VickiTuch

    The warning message keeps appearing because you've placed it as a part of a

    calculation script, which executes each time something changes in *any *of

    the form fields. It's not necessarily bad, since the used will be reminded

    of the problem until it's fixed.

     

    In the code of the button, just before the submitForm command, add the code

    I've provided before:

     

    if (this.getField("Total").value==100) {

    // place here the submitForm call that you currently have in your file

    } else {

    app.alert("Error! The total must be equal to 100.");

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points