Click here to Skip to main content
Licence CPOL
First Posted 26 Jun 2008
Views 18,818
Downloads 62
Bookmarked 18 times

Dynamic Javascript Validation Message Panel

By | 26 Jun 2008 | Article
This article describes creation of dynamic Javascript Message Panel that used to display validation failed error messages.

Introduction

During development we come across lots of client side validations and we normally use .Net validator controls.These controls give us the feature to display the client-side validation messages in 'Summary' like panel. In some places we need clean javascript validation that has to be used to serve our purpose.In such cases we want to display these messages not as alert message but to be displayed as inline form messages.

Background

Below is the javascript code that creates dynamic message panel.

function HideServerValidationPanel(panelID)
    {    
        if (document.getElementById(panelID) != null)
        {
            document.getElementById(panelID).style.display = "none";
        }        
    }
    function DisplayErrors(errorMessages)
    {        
        if (errorMessages != null && errorMessages.length > 0)
        {
             LoadValidationErrorPanel(errorMessages); 
             
             return false; 
        }
        else
        {
            return true;
        }
       
    }
    function LoadValidationErrorPanel(arrMessage)
    {
        
        var myMain =  document.getElementById ("MainErrorTag");
        if (myMain.firstChild != null)
        {
            myMain.removeChild(myMain.firstChild);
        }        
        var myDiv = document.createElement("div");
        var myUL = document.createElement("ul");
        var myPara =  document.createElement("p");
        var myImg = document.createElement("img");
        var myDesc = document.createElement("strong");
        
        myDiv.className = "ofWrapper";
        //myImg.className = "xxx";
        //myDesc.className ="yyy"

        myImg.setAttribute ("src","Cross.JPG");        
        myDesc.innerText = "Please correct the following details before proceeding ..." ;
       
        myPara.appendChild(myImg);
        
        myPara.appendChild(myDesc);
        myDiv.appendChild(myPara);
        var myLi;
        for(var j= 0; j < arrMessage.length;j++)
        {
            myLi = document.createElement("li");
            myLi.innerText = arrMessage[j];
           // myLi.className = "abc";
            myUL.appendChild(myLi);
        }
        myDiv.appendChild(myUL);
        myMain.appendChild(myDiv);
    }
    function SetFocus(ControlField, isFocusSet)
    {
        if (isFocusSet == false)
        {
            controlField.focus();
            return true;
        }
        else
            return false;
    }
    
    function ValidateMyScreen()
    {
        var Errors = new Array();
        var ErrorCounter = 0;
      
        if (document.getElementById('<%=TextBox1.ClientID%>').value == '')
        {
            Errors[ErrorCounter++] = "Data1 is required.";   
        }
          
        if (document.getElementById('<%=TextBox2.ClientID%>').value== '')
        {
            Errors[ErrorCounter++] = "Data2 is required.";   
        }    
              
    HideServerValidationPanel('<%=pnlMessage.ClientID%>');
    return DisplayErrors(Errors);
    }

Here is the html code where we have placed server side error message panel and client side DIV tag to display our messages. The dynamic javascript panel created at run time is appended to div tag ID ="MainErrorTag".

<asp:Panel runat="server" ID="pnlMessage">            
            <asp:Label ForeColor="Red" runat="server" ID="lblServerError"></asp:Label >            
        </asp:Panel>            
        <div id="MainErrorTag">            
        <div>

Conclusion

The above code is easy to use. One can create a style sheet to design message panel and create one common javascript file to use it across web pages.

License

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

About the Author

santosh poojari

Technical Lead

India India

Member

He is presently working as tech arch in one of the leading IT company.He has total 9 years of experience in C#.net. He is a B.E graduate in Computers from Bombay University.
 
Most of his experiences are in designing architect for end to end solutions. His interest areas are WCF,Spring.net,Architecture- Model View Presenter,UML,Webservice,Performance Engineering/tuning,Design patterns,Generics,Enterprise Library,Regular expressions,Silverlight and WWF.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberDhanoop719:14 29 Mar '12  
GeneralUse Existing Controls Pinmembernickyt7:27 9 Jul '08  
GeneralImprovements to Your JavaScript Pinmembernickyt3:50 7 Jul '08  
Santosh,
 
Here's some improvements for your JavaScript. There's more that can be improved, but here's some suggestions.
 
Later,
Nick Smile | :)
 
    function HideServerValidationPanel(panelID)
    {    
        var panel = document.getElementById(panelID); // or use $get if using ASP.NET AJAX
        
        if (panel)
            panel.style.display = "none";     
    }
    
    function DisplayErrors(errorMessages)
    {        
        //if (errorMessages != null && errorMessages.length > 0)
        // Checking the length > than zero is not necessary. 0 is interpreted as false
        // Checking errorMessages is null is redundant. An undefined object == false.
        var showErrors = errorMessages && errorMessages.length;
        if (showErrors)
             LoadValidationErrorPanel(errorMessages); 
             
        return !showErrors;       
    }
    
    function LoadValidationErrorPanel(arrMessage)
    {
        
        var myMain =  document.getElementById ("MainErrorTag");
        
        //if (myMain.firstChild != null) null check is redundant. An undefined object == false.
        if (myMain.firstChild)
            myMain.removeChild(myMain.firstChild);
    
        var myDiv = document.createElement("div");
        var myUL = document.createElement("ul");
        var myPara =  document.createElement("p");
        var myImg = document.createElement("img");
        var myDesc = document.createElement("strong");
        
        myDiv.className = "ofWrapper";
 
        myImg.setAttribute("src","Cross.JPG");        
        myDesc.innerText = "Please correct the following details before proceeding ..." ;
       
        myPara.appendChild(myImg);
        
        myPara.appendChild(myDesc);
        myDiv.appendChild(myPara);        
        var myLi;
        
        for(var j= 0; j < arrMessage.length;j++)
        {
            myLi = document.createElement("li");
            myLi.innerText = arrMessage[j];
            myUL.appendChild(myLi);
        }
        
        myDiv.appendChild(myUL);
        myMain.appendChild(myDiv);
    }
    
    function SetFocus(ControlField, isFocusSet)
    {
        //if (isFocusSet == false) == false is not required. Just do !isFocusSet
        if (!isFocusSet)
            controlField.focus();
 
        return !isFocusSet;
    }
    
    function ValidateMyScreen()
    {
        var Errors = new Array();
        var ErrorCounter = 0;
      
        if (document.getElementById('<%=TextBox1.ClientID%>').value == '')
        {
            Errors[ErrorCounter++] = "Data1 is required.";   
        }
          
        if (document.getElementById('<%=TextBox2.ClientID%>').value== '')
        {
            Errors[ErrorCounter++] = "Data2 is required.";   
        }    
              
    HideServerValidationPanel('<%=pnlMessage.ClientID%>');
    return DisplayErrors(Errors);
    }

 

GeneralRe: Improvements to Your JavaScript Pinmembersantosh poojari20:14 27 Jul '08  
GeneralNice Idea Pinmemberinetfly1232:05 27 Jun '08  
GeneralRe: Nice Idea Pinmembersantosh poojari17:58 29 Jun '08  
Generalgood artical PinmemberGautam Sharma23:31 26 Jun '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120604.1 | Last Updated 27 Jun 2008
Article Copyright 2008 by santosh poojari
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid