Skip to main content
Participant
May 3, 2017
Answered

Validation for max amounts and numerical increments

  • May 3, 2017
  • 1 reply
  • 1181 views

Hi there - I am creating a form that has two fields with unique validation requirements:

Field 1 - It must be an increment of $10,000 and cannot exceed $1,000,000.

Field 2 - It must be an increment of $10,000, and cannot exceed both Field 1 and $500,000.

Can anyone help with how I would write the validation script for these two fields? Please and many thanks. Mindy

This topic has been closed for replies.
Correct answer George_Johnson

Here's a custom validation script you can use for FIeld 1. Just tailor the error messages to suit you needs:

// Custom Validate script for Field 1

(function () {

    // Do nothing if value is blank

    if (!event.value) {

        return;

    }

    // Convert entry to a number

    var nVal = +event.value;

 

    // Deal with negative/zero values

    if (nVal <= 0) {

        app.alert("Please enter a positive value.", 3);

        // Reject entry

        event.rc = false;

        return;

    }

 

    // Deal with a value greater than 1,000,000

    if (nVal > 1000000) {

        app.alert("Please enter a value equal to or less than 1,000,000.", 3);

        event.rc = false;

        return;

    }

    // Deal with a value that's not a multiple of 10,000

    if (nVal % 10000) {

        app.alert("Please enter a value that is a multiple of 10,000.", 3);

        event.rc = false;

        return;

 

    }

 

    // Everything is OK, so nothing more to do

 

})();

For Field 2, the script could be modified to something like:

// Custom Validate script for Field 2

(function () {

    // Do nothing if value is blank

    if (!event.value) {

        return;

    }

    // Convert entry to a number

    var nVal = +event.value;

 

    // Get the value of Field 1, as a string

    var s1 = getField("Field 1").valueAsString;

 

    // Reject entry if field 1 is blank

    if (!s1) {

        app.alert("Please first enter a value in Field 1", 3);

        event.rc = false;

        return;

    }

    // Convert Field 1 value to a number

    var n1 = +s1;

    // Deal with a value greater than Field 1 value

    if (nVal > n1) {

        app.alert("Please enter a value less than or equal to Field 1", 3);

        event.rc = false;

        return;

    }

    // Deal with negative/zero values

    if (nVal <= 0) {

        app.alert("Please enter a positive value.", 3);

        event.rc = false;

        return;

    }

 

    // Deal with a value greater than 500,000

    if (nVal > 500000) {

        app.alert("Please enter a value equal to or less than 500,000.", 3);

        event.rc = false;

        return;

    }

    // Deal with a value that's not a multiple of 10,000

    if (nVal % 10000) {

        app.alert("Please enter a value that is a multiple of 10,000.", 3);

        event.rc = false;

        return;

 

    }

 

    // Everything is OK, so nothing more to do

 

})();

1 reply

Inspiring
May 3, 2017

Can you explain what you mean by "increment of $10,000"? If you mean a multiple of $10,000, (e.g., 10,000, 20,000, 30,000,..., 1,000,000), what do you want to happen if the user enters a value like 123,456?

If the user enters a value in Field 2, what should happen if Field 1 is blank?

Participant
May 3, 2017

Hi George! Thanks so much for the quick response and my apologies for not being more clear (this is the first time I've posted a question to a forum).  To answer your questions:

1) Yes, I mean that the user should only be able to input a number in multiples of $10,000.  If the user enters a value like $123,456 or enters a number greater than $1,000,000, I would like them to receive an error message like "Entry not valid.  Your election must be a multiple of $10,000 up to a maximum of $1,000,000."

2) If the user enters a value in Field 2 without an entry (blank) in Field 1, I would like the validation to treat Field 1 as if were "0", causing the user to receive an error message like "Entry not valid. Your election must be a multiple of $10,000 up to a maximum of $500,000 and cannot exceed your Field 1 election."  I would imagine that this same error message can be applied if the user makes an invalid entry for any of the rules.

Does this help?

George_JohnsonCorrect answer
Inspiring
May 3, 2017

Here's a custom validation script you can use for FIeld 1. Just tailor the error messages to suit you needs:

// Custom Validate script for Field 1

(function () {

    // Do nothing if value is blank

    if (!event.value) {

        return;

    }

    // Convert entry to a number

    var nVal = +event.value;

 

    // Deal with negative/zero values

    if (nVal <= 0) {

        app.alert("Please enter a positive value.", 3);

        // Reject entry

        event.rc = false;

        return;

    }

 

    // Deal with a value greater than 1,000,000

    if (nVal > 1000000) {

        app.alert("Please enter a value equal to or less than 1,000,000.", 3);

        event.rc = false;

        return;

    }

    // Deal with a value that's not a multiple of 10,000

    if (nVal % 10000) {

        app.alert("Please enter a value that is a multiple of 10,000.", 3);

        event.rc = false;

        return;

 

    }

 

    // Everything is OK, so nothing more to do

 

})();

For Field 2, the script could be modified to something like:

// Custom Validate script for Field 2

(function () {

    // Do nothing if value is blank

    if (!event.value) {

        return;

    }

    // Convert entry to a number

    var nVal = +event.value;

 

    // Get the value of Field 1, as a string

    var s1 = getField("Field 1").valueAsString;

 

    // Reject entry if field 1 is blank

    if (!s1) {

        app.alert("Please first enter a value in Field 1", 3);

        event.rc = false;

        return;

    }

    // Convert Field 1 value to a number

    var n1 = +s1;

    // Deal with a value greater than Field 1 value

    if (nVal > n1) {

        app.alert("Please enter a value less than or equal to Field 1", 3);

        event.rc = false;

        return;

    }

    // Deal with negative/zero values

    if (nVal <= 0) {

        app.alert("Please enter a positive value.", 3);

        event.rc = false;

        return;

    }

 

    // Deal with a value greater than 500,000

    if (nVal > 500000) {

        app.alert("Please enter a value equal to or less than 500,000.", 3);

        event.rc = false;

        return;

    }

    // Deal with a value that's not a multiple of 10,000

    if (nVal % 10000) {

        app.alert("Please enter a value that is a multiple of 10,000.", 3);

        event.rc = false;

        return;

 

    }

 

    // Everything is OK, so nothing more to do

 

})();