Click here to Skip to main content
Licence 
First Posted 15 Mar 2005
Views 21,657
Downloads 222
Bookmarked 11 times

ValidationScriptGenerator

By E KuralMani | 15 Mar 2005
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.
7 votes, 63.6%
1

2

3

4
4 votes, 36.4%
5
1.94/5 - 11 votes
μ 1.94, σa 3.53 [?]

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



United States United States

Member


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
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 15 Mar 2005
Article Copyright 2005 by E KuralMani
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid