Go to DotNetFunda.com
 Welcome, Guest!  
LoginLogin  
{ Submit content and get exposure !!! }
Submit: Article | Interview Question | Tips | Joke | Question | Link || Search  
 Skip Navigation Links Home > Articles > Java Script GridView ClientSide Validation

All Articles | Post Articles |  Subscribe to RSS

Java Script GridView ClientSide Validation

 Posted on: 9/30/2008 12:22:12 AM by Majith | Views: 1152 | Category: JavaScript | Level: Intermediate | Print Article
ASP.NET Hosting with Windows 2008/2003
I am going to explain the simple GridView Client Side Validation instead of server side validation controls.


GridView Client Side Validation
Introduction

Performing the Client side validation on GridView Itemtemplate controls is quite tedious and finding the controls it seems to difficult.This snippet below validates all controls using JS.The article explains the GridView Client Side Validation instead of server side validation controls.

HTML Code

<title>Java Script GridView ClientSide Validation</title>
<script language="javascript" type="text/javascript">
  function check()
   {
    var grid = document.getElementById('<%= GridView1.ClientID %>');
     if(grid!=null)
      {
       var Inputs = grid.getElementsByTagName("input");
        for(i = 0; i < Inputs.length; i++)
         {
          if(Inputs[i].type == 'text' )
           {
            if(Inputs[i].id == 'GridView1_ctl02_txtName')
             {
              if(Inputs[i].value=="")
               {
                alert("Enter Name ");
                return;
               }
             }
         if(Inputs[i].id == 'GridView1_ctl02_txtEmail')
             {
                if(Inputs[i].value=="")
               {
                alert("Enter Email");
                  return;
               }
               var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
               var emailid =Inputs[i].value;
               var matchArray = emailid.match(emailPat);
               if (matchArray == null)
                 {
                   alert("Your email address is not valid.");
                   return; 
                 }
              }
            }        }
    }
}
</script>  

1. First of all I am finding the Giridview using the ClientID , then the type of controls Input and the ID of the Controls.

2.The GridElementsByTagName returns the unique Id of the controls (For Ex:'GridView_ctlo2_txtName')

3.Using this Id we have to check the null valus and other validations.

GridView Code
<form runat="server">
 <asp:Button ID="btnEdit" runat="server" OnClick="btnEdit_Click" Text="NEW" />
    <asp:Button ID="Button1" runat="server" OnClick="btnCancel_Click" Text="CANCEL" style="position: relative" />
        <asp:GridView ID="GridView1" runat="server"
          AutoGenerateColumns="false"
           OnRowEditing="GridView1_RowEditing" 
           OnRowCommand="GridView1_RowCommand" 
        Style="left: 324px; position: relative;         
            top: 86px" Width="308px">
            <Columns>
            <asp:TemplateField HeaderText="Name"> 
            <ItemTemplate>
            <table>
            <tr>
            <td>
             <asp:TextBox ID="txtName"  Visible="false"  runat="server"></asp:TextBox>
            </td>
            </tr>
            <tr>
            <td>
             <asp:Label ID="lblname" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"name")%>'></asp:Label>
            </td>
            </tr>
            </table>
            </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Email"> 
             <ItemTemplate>
             <table>
            <tr>
            <td>
            <asp:TextBox ID="txtEmail" Visible="false" runat="server"></asp:TextBox> 
           </td>
            </tr>
            <tr>
            <td>
           <asp:Label ID="lblemail" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"eml")%>'></asp:Label>
            </td>
            </tr>
            </table>
            </ItemTemplate>
            </asp:TemplateField>
           </Columns>           
        </asp:GridView>   
    </div>
    </form>

4.On the Page_load event add the Button.Attributes.Add onclick event client function.

Code Behind
 protected void Page_Load(object sender, EventArgs e)
    {
        btnEdit.Attributes.Add("onclick", "return check()");  
    }
 
Conclusion
 Hope the above code helps to the developers who are un known to  GridView Client side Validations.


Interesting?  Bookmark and Share kick it on DotNetKicks.com


About Majith B

Experience:3 year(s)
Home page:
Member since:Friday, July 18, 2008
Biography:
 Latest post(s) from Majith

   ◘ Java Script GridView ClientSide Validation posted on 9/30/2008 12:22:12 AM
   ◘ Diff Between DataGrid and GridView posted on 9/17/2008 11:20:35 PM
   ◘ Dynamic Single click , Double Click for Editing the rows in Gridview posted on 9/10/2008 11:44:57 PM
   ◘ Applying Themes and Skins using ASP.NET ,C# posted on 8/21/2008 3:24:56 AM
   ◘ Dynamic Menu using XML DataSource ASP.NET ,VB.NET posted on 8/6/2008 10:55:31 PM




About Us | Contact Us | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
All rights reserved to DotNetFunda.com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)