Click here to Skip to main content
15,885,435 members
Articles / Web Development / HTML

Multiple Validation Groups

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
10 Dec 2012CPOL 8.3K   4   1
Multiple validation groups

Sometimes, it is a requirement to have separate validation groups to be triggered for an action. To meet the requirement, you can simply write some JavaScript code to perform validation according to the specific condition.

For example:

JavaScript
function IsSearchInputValidated() {
    return (ClientValidate("Search") & ClientValidate("AdvancedSearch"));
}

or:

JavaScript
function IsSearchInputValidated() {
    return (ClientValidate("Search") && ClientValidate("AdvancedSearch"));
}

If you were to write the code in this way, you will get only 1 of the validation groups message displayed. ^^

To overcome this, we can create a helper function to make sure that all validation groups are being validated at one shot.

JavaScript
function ClientValidate(groupName) {
    var result = new Boolean(1);
    var g = groupName.split(";");
    var html = new String();

    for (var i = 0; i < g.length; i++) {
        if (!Page_ClientValidate(g[i])) {//not valid.
            result = false;
            if (typeof (Page_ValidationSummaries) != "undefined") {
                for (var j = 0; j < Page_ValidationSummaries.length; j++) {
                    if (Page_ValidationSummaries[j].validationGroup == g[i]) {
                        //Use 'html' variable to keep the previous validation summaries 
                        //and display together with the current validation summaries.
                        html += Page_ValidationSummaries[j].innerHTML;
                        break;
                    }
                }
            }
        }
    }

    if (result == false) {
        //Clear others summary and display the validation summaries.
        if (typeof (Page_ValidationSummaries) != "undefined") {
            for (var i = 0; i < Page_ValidationSummaries.length; i++) {
                for (var j = 0; j < g.length; j++) {
                    if (Page_ValidationSummaries[i].validationGroup == g[j]) {
                        Page_ValidationSummaries[i].innerHTML = html;
                        Page_ValidationSummaries[i].style.display = "inline";
                        //"none"; "inline";
                    }
                    else {
                        Page_ValidationSummaries[i].innerHTML = "";
                    }
                }
            }
        }
    }

    return result;
};

And the code to consume it will look like this:

JavaScript
function IsSearchInputValidated() {

    var option = document.getElementById("AdvancedSearch");

    if (option.checked == true) {
        return ClientValidate("Search;AdvancedSearch");
    }
    else {
        return ClientValidate("Search");
    }
}

See, now we will have both validation groups message displayed.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Powercomp Software Sdn Bhd
Malaysia Malaysia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDisplay message near validator Pin
Ugo Basile1-Mar-18 0:20
Ugo Basile1-Mar-18 0:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.