eService: Entity Editor Field Validation

Mar 22, 2010 at 3:25 PM

I found that the Javascript for the Entity Editor field validation (EntityEditor.ascx.js) doesn't work for browsers other than IE.  For anyone interested, here are my modifications to handle other browsers:

function VerifyInput(entityName)
{
    var fields = [];

    var allTables = document.getElementsByTagName("TABLE");

    // First, save all of the fields that require validation in the fields array
    for (var i = 0, tableLength = allTables.length; i < tableLength; i++)
    {
        var currentTable = allTables[i];

        //Changed to work on browsers other than IE
        var entityAttribute = currentTable.getAttribute("entity");
        if (entityAttribute == null) continue;
        if (entityAttribute == entityName.toLowerCase())
        //
        {
            var tableChildren = currentTable.getElementsByTagName("*");

            for (var j = 0, childrenLength = tableChildren.length; j < childrenLength; j++)
            {
                var child = tableChildren[j];
                //Changed to work on browsers other than IE
                var requiredAttribute = child.getAttribute("req");
                if (requiredAttribute == null) continue;
                if (requiredAttribute == "1" || (requiredAttribute == "0" && child.value != "undefined" && child.value != null && child.value != ""))
                //
                {
                    fields.push(child);
                }
            }
        }
    }

    // Loop through and make sure all required fields have data.
    for (var i = 0; i < fields.length; i++)
    {
        var field = fields[i];

        //Changed to work on browsers other than IE
        var fieldRequiredAttribute = field.getAttribute("req");
        if (fieldRequiredAttribute == null) continue;
        if (fieldRequiredAttribute == "1" && (field.value == null || field.value == ""))
        //
        {        
            // If there is a notification label, we'll display the note in that.
            // Otherwise, we'll alert the user via messagebox.					
            if (window["notificationLabelId"])
            {
                var label = document.getElementById(window["notificationLabelId"]);

                label.style.display = "";
                label.className = "msa-Global-NotificationLabel-Error";
                //Changed to work on browsers other than IE
                label.innerText = document.requiredFieldMessage.replace("{0}", field.getAttribute("attributeLabel"));
                return false;
            }
            else
            {
                //Changed to work on browsers other than IE
                alert(document.requiredFieldMessage.replace("{0}", field.getAttribute("attributeLabel")));
                return false;
            }
        }
    }

    // Loop through again and validate all date fields
    for (var i = 0; i < fields.length; i++)
    {
        var field = fields[i];

        var mdyRegEx = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})))/;
        var dmyRegEx = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
        var ymdRegEx = /^(?=\d)(?:(?:(?:(?:1[6-9]|[2-9]\d)?\d{2})(\/|-|\.)(?:(?:(?:(?:0?[13578]|1[02])\1(?:31))|(?:(?:0?[1,3-9]|1[0-2])\1(?:29|30))))|(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))(\/|-|\.)0?2\2(?:29))|(?:(?:1[6-9]|[2-9]\d)?\d{2})(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\3(?:0?[1-9]|1\d|2[0-8])))?$/;

        var showInvalidDateMessage = false;

        if (field.attributeType == "DateTime")
        {
            var ddmmyyyy = /d+[\.\-\/]m+[\.\-\/]y+/;
            var mmddyyyy = /m+[\.\-\/]d+[\.\-\/]y+/;
            var yyyymmdd = /y+[\.\-\/]m+[\.\-\/]d+/;

            if (document.dateFormat.match(ddmmyyyy) && !field.value.match(dmyRegEx))
            {
                showInvalidDateMessage = true;
            }
            else if (document.dateFormat.match(mmddyyyy) && !field.value.match(mdyRegEx))
            {
                showInvalidDateMessage = true;
            }
            else if (document.dateFormat.match(yyyymmdd) && !field.value.match(ymdRegEx))
            {
                showInvalidDateMessage = true;
            }
        }

        if (showInvalidDateMessage)
        {
            // If there is a notification label, we'll display the note in that.
            // Otherwise, we'll alert the user via messagebox.					
            if (window["notificationLabelId"])
            {
                var label = document.getElementById(window["notificationLabelId"]);

                label.style.display = "";
                label.className = "msa-Global-NotificationLabel-Error";                
                label.innerText = document.dateErrorMessage;
                return false;
            }
            else
            {
                alert(document.dateErrorMessage);
                return false;
            }
        }
    }

    return true;
}