You are viewing limited content. For full access, please sign in.

Question

Question

Feature Request: Field Rule Validation For Unique Value

asked on October 29

In the Forms Layout Designer, you can create Field Rules to validate values that are entered. I would like to be able to use this to verify that the value entered in a table or collection field is unique compared to the other rows for the same field.

For example, I have a table where there is a field named ID. This field performs a lookup to populate other fields in the same table row. I don't want the end user to be able to enter the same ID value in two different rows and if they do, give them an error message.

I know this can be done in other ways, but it would be nice if it was a built-in field validation option.

2 0

Replies

replied on October 29

Hi Blake, I don't know if this can help you or at least give you an idea or a guide. 

Try the following:

$(document).ready(function () {
    // Define the table selector
    var tableSelector = '#q4';
    // Define the ID field selector pattern for all rows inside the table
    var idFieldSelector = '[id^="Field1("]'; // Matches fields with IDs like q4(1), q4(2), etc.

    function validateUniqueIDs() {
        var ids = [];
        var isUnique = true;

        // Iterate over each ID field within the table
        $(tableSelector).find(idFieldSelector).each(function () {
            var idValue = $(this).val().trim();
            if (idValue) {
                if (ids.includes(idValue)) {
                    isUnique = false;
                    return false; // Stop the loop if a duplicate is found
                }
                ids.push(idValue);
            }
        });
        return isUnique;
    }

    // Function to show an error alert and clear the duplicate value
    function showErrorAndClear(field) {
        alert("Each ID must be unique. Please enter a different value.");
        field.val(''); // Clear the duplicate entry
    }

    // Validate on input in each ID field, including newly added rows
    $(tableSelector).on('input', idFieldSelector, function () {
        if (!validateUniqueIDs()) {
            showErrorAndClear($(this));
        }
    });

    // Validate on adding a new row
    $(tableSelector).on('click', '.add-row-button', function () {
        setTimeout(function () {
            // Re-bind validation to the ID field in any new rows added
            $(tableSelector).find(idFieldSelector).off('input').on('input', function () {
                if (!validateUniqueIDs()) {
                    showErrorAndClear($(this));
                }
            });
        }, 100); // Wait briefly to ensure the new row is in the DOM
    });
});

Explanation:

  1. Selectors:
    • tableSelector = '#q4': Targets the table with the ID q4. "You must replaced by your own tableSelector ID" in my test was q4.
    • idFieldSelector = '[id^="Field1"]': Targets all fields inside the table whose IDs start with Field1, covering all rows like Field1(1), Field1(2), etc. "You must find and change according your ID field id Selector."
  2. Event Binding:
    • Binds the validation function to any existing and newly added rows, ensuring that every time a value is entered in the Field1 ID field, it checks for duplicates across all rows.

This will now validate unique IDs in all Field1 fields within #q4, including any newly added rows. If a duplicate value is detected, the script displays an alert and clears the duplicated field.

 

The form I made to test this: 

 

The form in preview: 

That message popup when trying to duplicate ID number 2

I hope this helps you.

1 0
replied on October 29

Luis, I appreciate the descriptive reply. This was more of just a feature request though so it can be added to the interface.

1 0
replied on October 29

Oh, I didn't even notice, sorry I was so focused on helping that I didn't pay attention to your intention.

0 0
replied on October 29

Not a problem. I'm sure I'll come back to your reply and use it at some point, and it will probably be helpful for others that are looking to do the same thing.

1 0
replied on October 29

I hope it is useful to anyone who may find it useful. Thank you!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.