Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
How to jquery validate gridview inside databound textbox value to check null and duplicate entry?

I am try this way:

how to loop to gridview have 15 records and to check only modulename not null and no duplicate modulename?

jquery:

C#
<head  runat="server">
<script type="text/jscript">
   $(document).ready(function () {
        function validate() {
            alert("please enter the module name1");

            //if ($(".validationClass" == ''))
            //    alert("please enter the module name3");
           //}


            var iVal = $('#gdvCRLevels_ctl03_txtModuleName').val();
                if (iVal == 0) {
                    alert("please enter 3");
            }


        });
    </script>
</head>


asp.net

C#
<table><tr>
<td id="tdRowGrid" style="width: 75pc" valign="top" align="center"  runat="server">
	<asp:GridView ID="gdvCRLevels" runat="server" BorderWidth="0px" CellPadding="3" CellSpacing="1"
		Visible="true" CssClass="DataGrid" OnRowDataBound="gdvCRLevels_RowDataBound"
		AllowPaging="false" AutoGenerateColumns="false" Width="100%" AlternatingRowStyle-CssClass="alternateItem">
		<RowStyle CssClass="NormalItem" VerticalAlign="Top" />
		<HeaderStyle CssClass="CellHeaderEven" HorizontalAlign="Left" />
		<PagerStyle CssClass="CellHeaderEven" HorizontalAlign="Center" />
		<Columns>
			<asp:BoundField DataField="Module Title">
				<ControlStyle Width="300px" />
				<HeaderStyle HorizontalAlign="Center" />
				<ItemStyle ForeColor="#009900" Font-Bold="True" />
			</asp:BoundField>
			<asp:BoundField DataField="CRModuleId"></asp:BoundField>
			<asp:TemplateField>
				<HeaderStyle HorizontalAlign="Center" />
				<ControlStyle Width="200px" />
				<ItemTemplate>
					<asp:TextBox ID="txtModuleName" runat="server" CssClass="validationClass" />
				</ItemTemplate>
			</asp:TemplateField>
			<asp:TemplateField>
				<HeaderStyle HorizontalAlign="Center" />
				<ControlStyle Width="300px" />
				<ItemTemplate>
					<asp:TextBox ID="txtReferenceCourseName" runat="server" />
				</ItemTemplate>
			</asp:TemplateField>
			<asp:TemplateField>
				<HeaderStyle HorizontalAlign="Center" />
				<ControlStyle Width="100px" />
				<ItemTemplate>
					<asp:TextBox ID="txtModuleCode" runat="server" />
				</ItemTemplate>
			</asp:TemplateField>
		</Columns>
	</asp:GridView>
</td></tr>
</table>


c#
.cs


C#
protected void gdvCRLevels_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Variables
        if ((e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
        {
            e.Row.Cells[1].Visible = false;

            e.Row.Cells[0].Width = Unit.Percentage(200);
            e.Row.Cells[2].Width = Unit.Percentage(100);
            e.Row.Cells[3].Width = Unit.Percentage(200);
            e.Row.Cells[4].Width = Unit.Percentage(50);
        }
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Text = "Module Title";
            e.Row.Cells[2].Text = "Module Name";
            e.Row.Cells[3].Text = "Reference Course Name";
            e.Row.Cells[4].Text = "Module Code";
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox objtextBox1 = (TextBox)e.Row.Cells[2].FindControl("txtModuleName");
            objtextBox1.Text = DataBinder.Eval(e.Row.DataItem, "Module Name").ToString();

            TextBox objtextBox2 = (TextBox)e.Row.Cells[3].FindControl("txtReferenceCourseName");
            objtextBox2.Text = DataBinder.Eval(e.Row.DataItem, "Reference Course Name").ToString();

            TextBox objtextBox3 = (TextBox)e.Row.Cells[4].FindControl("txtModuleCode");
            objtextBox3.Text = DataBinder.Eval(e.Row.DataItem, "Module Code").ToString();
        }
    }


Thanks,
Karthikeyan,Bangalore
Posted
Updated 26-Mar-14 2:31am
v3
Comments
ZurdoDev 26-Mar-14 8:57am    
This is not clear at all. What is clear is you want to validate your values using jquery.

Look at the source html and see if all the fields you want to validate have something in common, for example, if they all have the same css class or start with a certain name, etc. Then you can use a jquery selector to get them all and use .each() to loop through them all.

1 solution

You have to loop through the Rows of GridView, which is rendered as a Table on Browser.

Then inside the loop, find the input fields containing txtModuleName in the ID attribute by Attribute Contains Selector [name*="value"][^].
JavaScript
var previousModuleName = new Array();
var count = 0;
previousModuleName[count] = "";

function validate() {
    $("#gdvCRLevels tr").each(function () {
        var txtModuleName = $(this).find("input[id*='txtModuleName']");

        if (txtModuleName.length > 0) {
            if (txtModuleName.val().length === 0) {
                alert("You must specify ModuleName");
            }

            if (jQuery.inArray(txtModuleName.val().toLowerCase(), previousModuleName) > -1) {
                alert("Duplicate Entry");
            }
        
            count++;
            previousModuleName[count] = txtModuleName.val().toLowerCase();
        }       
    });
}
 
Share this answer
 
v4
Comments
pkarthionline 27-Mar-14 0:36am    
thanks your helps.it' working good.
and how to check duplicate?
What exactly you mean by duplicate? Can you explain with an example?
pkarthionline 27-Mar-14 2:08am    
when i am saving (gridview) data should not duplicate.jquery code

example:

Module1
Module2
Module3
module2
module3
Module5
Module6
Module4

Here,

Module2,Module3 duplicate .when i am save time should display msg "duplicate entry"
Okay, check my updated Solution now. :)
Customize according to your requirements.
Hold on. I am modifying it again. :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900