I have a GridView in Which I have four TextBoxes in the Template Field. I have one Button below the GridView.
How to validate the TextBoxes in the GridView, When the Button Clicked?
use RequiredFieldValidator and set ValidationGroup="gridview", check below example
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="TextBox3" ValidationGroup="gridview" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="Button2" runat="server" Text="Button" ValidationGroup="gridview" CausesValidation="true" />
</ItemTemplate>
</asp:TemplateField>
You can use the JQuery Validation Plugin
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate /lib/jquery.delegate.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>
<script type="text/javascript">
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});;
</script>
<script>
$(document).ready(function(){
$("#myform").validate({
rules: {
field: "required"
}
});
});
<body>
<form id="myform">
<label for="field">Required: </label>
<input class="left" id="field" name="field" />
<br/>
<input type="submit" value="Validate!" />
</form>
</body>
<script type="text/javascript">
function ValidateGridview() {
titlename = document.getElementById('<%=((TextBox)grd_party_influenc.FooterRow.FindControl("txt_f_title")).ClientID%>');
if (titlename.value ==0) {
alert("Please Insert The Title ....");
titlename.focus();
return false;
}
// return true;
}
</script>
And then call the JavaScript function through link button like this:
<asp:LinkButton ID="lnk_btn_insert" runat="server" CommandName="Insert" OnClientClick="ValidateGridview()">Insert</asp:LinkButton>
i have 7 textbox
ok i have worked on my JS function and it worked for me.hope it will work for everyone else.in my code i have taken a variable
successwhich is working as a flag and i am checking it twice and returningtrueat the end so that if one of the textbox is not empty and other not it will not return true. sorry for poor editing
function fnCheck(val) {
var success = true;
var v = val.id.split('_')[1];
var merter = document.getElementById('GridSubMeter_' + v + '_txtMeterIdn').value.trim();
var Billper = document.getElementById('GridSubMeter_' + v + '_txBillPer').value.trim()
var Endkwh = document.getElementById('GridSubMeter_' + v + '_txEndKwh').value.trim();
var startkwh = document.getElementById('GridSubMeter_' + v + '_txStartKwh').value.trim();
var ReadEndDate = document.getElementById('GridSubMeter_' + v + '_txReadEndDate').value.trim();
var ReadStartDate = document.getElementById('GridSubMeter_' + v + '_txReadStartDate').value.trim();
var CTFACT = document.getElementById('GridSubMeter_' + v + '_txCTFact').value.trim();
debugger;
if (merter != '') {
}
else {
alert("Meter Identifier is Required Field");
success = false;
}
if (Billper != '') {
}
else {
alert("Bill Period is Required Field");
success = false;
}
if (Endkwh != '') {
}
else {
alert("EndKwh is Required Field");
success = false;
}
if (startkwh != '') {
}
else {
alert("StartKwh is Required Field");
success = false;
}
if (ReadEndDate != '') {
}
else {
alert("Read EndDate is Required Field");
success = false;
}
if (ReadStartDate != '') {
}
else {
alert("Read StartDate is Required Field");
success = false;
}
if (CTFACT != '') {
}
else
{ alert("CT Factor is Required Field");
success = false;
}
return success;
}
onclientclick
<asp:Button ID="btn_Update" Style="background-color: #B2DE94; width: 40px" CausesValidation="false" runat="server" OnClientClick="return fnCheck(this);" Text="Update" CommandName="Update" />