Validating Taxonomy Metadata Field in Javascript (Multiple Value Field)

In my previous post, I had mentioned about performing validation of a single value taxonomy field. This script does validation of a multi-value field.

This and the previous script works for how many ever taxonomy fields you have in the edit form. Now what I am trying to do is validate and prevent the user from saving the document if the same value repeats in those fields.
*******************************

<script type="text/javascript">
        function PreSaveAction() {
            //alert("calling before save")

            var iRef = document.getElementsByTagName("div");

            var arr = new Array();
            var duplicate = false;
            var errors = new Array();

            for (var i = 0; i < iRef.length; i++) {
                if (iRef[i].id.indexOf("editableRegion") >= 0) {
                    var itemFound = iRef[i];
                    if (itemFound.innerText.length == 1) {
                        continue;
                    }

                    var splitItems = itemFound.innerText.split(";");
                    for (k = 0; k < splitItems.length; k++) {
                        for (var j = 0; j < arr.length; j++) {

                            if (splitItems[k].length > 1) {
                                if (arr[j].indexOf(splitItems[k].replace(/^\s+/, "")) >= 0) {
                                    duplicate = true;
                                    var foundInArray = false;
                                    for (var m = 0; m < errors.length; m++) {
                                        if (errors[m] == splitItems[k])
                                        { foundInArray = true; }
                                    }
                                    if (!foundInArray) {
                                        errors.push(splitItems[k]);


                                    }
                                    // return false;
                                }
                            }

                        }
                    }
                    arr.push(itemFound.innerText);


                }
            }
            if (duplicate) {
                alert("Classification (" + errors.join(',') + ") already exists! \n\nPlease choose a different classification.")

                return false;
            }
            return true;
        }</script>

Comments

Popular posts from this blog

Using External Content Types with Stored Procedures with Input Parameters

Comparison of Power BI Service vs. Power BI Report Server vs. SQL Server Reporting Services