5,696,038 members and growing! (11,219 online)
Email Password   helpLost your password?
Web Development » Validation » General     Intermediate

ValidationScriptGenerator

By E KuralMani

This article gives you a solution for generating a client side validation script in a component model that you can reuse in an ASP.NET project.
C#, Windows, .NET, Visual Studio, ASP.NET, Dev

Posted: 15 Mar 2005
Updated: 15 Mar 2005
Views: 15,358
Bookmarked: 6 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
11 votes for this Article.
Popularity: 2.02 Rating: 1.94 out of 5
7 votes, 63.6%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
4 votes, 36.4%
5

Summary

This article gives you a solution for generating a client side validation script in a component model that you can reuse in an ASP.NET project.

Introduction

Every web based application should have a client side validation to reduce the load in server. It is not a good programming model to write a code in each and every page. The code should be reusable for the entire application. For this, I have came up with a solution that will help you to generate client side validation script with very less code.

Requirements

  • .NET Framework 1.1

Step by step procedure

The below is the procedure to use this component:

  • Add the reference of the DLL (ValidationScriptGenerator) to your project that has the aspx pages.
  • Include the js file (Functions.js) in the aspx page.
  • Create a ListDictionary object and add the ID of the control and message to be displayed.
  • Pass your ListDictionary object to the static method GetScriptBlock that will return a string.
  • Pass the returned string into the static method RegisterScriptBlock with your button ID as another parameter.

Code Summary

The below is the code for adding your controls to ListDictionary.

ListDictionary objLDControls=new ListDictionary() 
objLDControls.Add("First Name cannot be blank",this.txtFirstName.ID); 
objLDControls.Add("Gender cannot be blank",this.ddlGender.ID); 
objLDControls.Add("Description cannot be blank",this.txtDesc.ID); 

Don’t forget to include the namespace System.Collections.Specialized since the ListDictionary belong to this namespace. Reason for choosing ListDictionary is to maintain the order (FIFO) of the controls (no other collection will maintain the order).

Public Methods Used

GetScriptBlock

This method is used to generate the script block.

public static string GetScriptBlock(Page page,ListDictionary htControls,
                               string functionName,int pageSpecificScript)
{
  string scriptBlock=null;
  string strAlertMessage=string.Empty;
                  
  formName=GetFormName(page);
  if(formName!=null)
  {
    foreach(DictionaryEntry deEntry in htControls)
    {
      if(page.FindControl(deEntry.Value.ToString()) is TextBox)
      {
        TextBox  regControl=(TextBox)page.FindControl(deEntry.Value.ToString());
                                    
        scriptBlock=scriptBlock+GenerateTextBoxScript(deEntry,regControl,
                             pageSpecificScript);
      }
                                    
      else if(page.FindControl(deEntry.Value.ToString()) is DropDownList)
      {
        DropDownList regControl=
           (DropDownList)page.FindControl(deEntry.Value.ToString());
        scriptBlock= scriptBlock + GenerateDropDownScript(deEntry,regControl,
                                                  pageSpecificScript);
      }     
      else if(page.FindControl(deEntry.Value.ToString()) is HtmlInputText)
      {
        HtmlInputText regControl=
           (HtmlInputText)page.FindControl(deEntry.Value.ToString());
        scriptBlock=
           scriptBlock+GenerateTextBoxScript(deEntry,
           regControl,pageSpecificScript);
 
      }
    }
    if(functionName!=null) scriptBlock=scriptBlock+functionName+";";
  }
                  
   return scriptBlock;
}

RegisterScriptBlock

This method is used to register the script block to the control.

public static void RegisterScriptBlock(Control btnHolder,
                              Page page,string scriptBlock)
{
                  
                  
  if(btnHolder is ImageButton)
  {
    ImageButton btnSource=(ImageButton)btnHolder;
    btnSource.Attributes["onclick"]="javascript:"+scriptBlock;
 
    HttpResponse myHttpResponse = HttpContext.Current.Response;
    HtmlTextWriter myHtmlTextWriter  = 
           new HtmlTextWriter(myHttpResponse.Output);
 
    btnSource.Attributes.AddAttributes(myHtmlTextWriter);
  }
  else if(btnHolder is Button)
  {
    Button btnSource=(Button)btnHolder;
    btnSource.Attributes["onclick"]="javascript:"+scriptBlock;
 
    HttpResponse myHttpResponse = HttpContext.Current.Response;
    HtmlTextWriter myHtmlTextWriter  = 
         new HtmlTextWriter(myHttpResponse.Output);
 
    btnSource.Attributes.AddAttributes(myHtmlTextWriter);
  }
                  
}

Private Methods Used

GenerateTextBoxScript

This method is used to generate script for TextBox control.

private static string GenerateTextBoxScript(string message,TextBox regControl) 
{ 
  //The trim function is in the Functions.js file 

  //it has to be included in the aspx file 

  string line=null; 
  line="trim("+formName+"."+regControl.ID+@".value)=='' || "+formName+"."+
                              regControl.ID+@".value=='12/31/2025'"; 
  return "if("+line+"){ alert('"+message+"');"+formName+"."+
                              regControl.ID+@".focus();return false}"; 
  
}

GenerateDropDownScript

This method is used to generate script for DropDown control.

private static string GenerateDropDownScript(DictionaryEntry deEntry,
                       DropDownList regControl,int pageSpecificScript)
{
  string line=null;
  string result=null;
  line=formName+"."+regControl.ID+@".selectedIndex=='0'";
  result= "if("+line+"){ alert('"+deEntry.Key.ToString()+"');"+formName+"."+
                      regControl.ID+@".focus();return false}";
  return result; 
}

Limitations

  • This component will validate only empty fields.
  • The first element of the Dropdown should have the value 0 (i.e. <Select> value should be ‘0’).

Conclusion

My intention of this article is not to reinvent the wheel. Use this component to cut down development time. Meanwhile I will enhance this component for more validation.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

E KuralMani



Location: United States United States

Other popular Validation articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Mar 2005
Editor: Sumalatha K.R.
Copyright 2005 by E KuralMani
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project