The below code validates all the textboxes in a Gridview using a single CustomValidator control
function isValidDate(sText) {
var reDate = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/;
if(sText.length==0)
return false;
else
return reDate.test(sText);
}
function isValidDecimalValue(sValue)
{
var reDecimal = /^\d{1,12}(\.(\d{1,2}))?$/;
return reDecimal.test(sValue);
}
function validateForm(source, value)
{
var isXDDateError = false;
var isGrossError = false;
var objError = document.getElementById("<%= spnError.ClientID %>");
objError.innerHTML="";
isXDDateError=ValidateDateValue();
isGrossError=ValidateGross();
if(isXDDateError || isGrossError)//Both false
{
value.IsValid = false;
}
else
value.IsValid = true;
}
function ValidateGross()
{
var obj = document.getElementsByTagName("input");
var objError = document.getElementById("<%= spnError.ClientID %>");
var objSpan, isError = false;
for(i=0; i<obj.length; i++)
{
if(obj[i].name.indexOf("txtGross") > 0)
{
if(!isValidDecimalValue(obj[i].value))
{
isError = true;
if(obj[i].parentNode != null)
{
for(j=0; j < obj[i].parentNode.childNodes.length; j++)
{
objSpan = obj[i].parentNode.childNodes[j];
if( objSpan != null && objSpan.id != undefined)
{
if(objSpan.id.indexOf("spnGross") == 0)
{
objSpan.innerText = "*";
}
}
}
}
}
else
{
if(obj[i].parentNode != null)
{
for(j=0; j < obj[i].parentNode.childNodes.length; j++)
{
objSpan = obj[i].parentNode.childNodes[j];
if( objSpan != null && objSpan.id != undefined)
{
if(objSpan.id.indexOf("spnGross") == 0)
{
objSpan.innerText = "";
}
}
}
}
}
}
}
if(isError)
{
if(objError.innerHTML.length>0)
objError.innerHTML+="* Please enter a value for Gross.</br>"
else
objError.innerHTML="* Please enter a value for Gross.</br>"
objError.display="block";
}
return isError;
}
<span id="spnError" runat="server" style="color:Red"></span>
<asp:CustomValidator ID = "CustomValidator1" runat = "server" ClientValidationFunction = "validateForm"></asp:CustomValidator>
<asp:Button ID="btnSave" CausesValidation="true" runat="server" Visible="true" class="tdsButton" Text="Save"
align="right" OnClick="btnSave_Click" />
<asp:GridView ID="HoldingsGrid" runat="server">
<Columns>
<asp:TemplateField HeaderText="Gross" SortExpression="SortOnGross">
<ItemTemplate>
<asp:Label ID="lblGross" runat="server" Text='<%# Bind("PAYMENT_GROSS_LOCAL_CUST") %>' />
<asp:TextBox ID="txtGross" runat="server" />
<span id="spnGross" style="color:Red" ></span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
function isValidDate(sText) {
var reDate = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/;
if(sText.length==0)
return false;
else
return reDate.test(sText);
}
function isValidDecimalValue(sValue)
{
var reDecimal = /^\d{1,12}(\.(\d{1,2}))?$/;
return reDecimal.test(sValue);
}
function validateForm(source, value)
{
var isXDDateError = false;
var isGrossError = false;
var objError = document.getElementById("<%= spnError.ClientID %>");
objError.innerHTML="";
isXDDateError=ValidateDateValue();
isGrossError=ValidateGross();
if(isXDDateError || isGrossError)//Both false
{
value.IsValid = false;
}
else
value.IsValid = true;
}
function ValidateGross()
{
var obj = document.getElementsByTagName("input");
var objError = document.getElementById("<%= spnError.ClientID %>");
var objSpan, isError = false;
for(i=0; i<obj.length; i++)
{
if(obj[i].name.indexOf("txtGross") > 0)
{
if(!isValidDecimalValue(obj[i].value))
{
isError = true;
if(obj[i].parentNode != null)
{
for(j=0; j < obj[i].parentNode.childNodes.length; j++)
{
objSpan = obj[i].parentNode.childNodes[j];
if( objSpan != null && objSpan.id != undefined)
{
if(objSpan.id.indexOf("spnGross") == 0)
{
objSpan.innerText = "*";
}
}
}
}
}
else
{
if(obj[i].parentNode != null)
{
for(j=0; j < obj[i].parentNode.childNodes.length; j++)
{
objSpan = obj[i].parentNode.childNodes[j];
if( objSpan != null && objSpan.id != undefined)
{
if(objSpan.id.indexOf("spnGross") == 0)
{
objSpan.innerText = "";
}
}
}
}
}
}
}
if(isError)
{
if(objError.innerHTML.length>0)
objError.innerHTML+="* Please enter a value for Gross.</br>"
else
objError.innerHTML="* Please enter a value for Gross.</br>"
objError.display="block";
}
return isError;
}
<span id="spnError" runat="server" style="color:Red"></span>
<asp:CustomValidator ID = "CustomValidator1" runat = "server" ClientValidationFunction = "validateForm"></asp:CustomValidator>
<asp:Button ID="btnSave" CausesValidation="true" runat="server" Visible="true" class="tdsButton" Text="Save"
align="right" OnClick="btnSave_Click" />
<asp:GridView ID="HoldingsGrid" runat="server">
<Columns>
<asp:TemplateField HeaderText="Gross" SortExpression="SortOnGross">
<ItemTemplate>
<asp:Label ID="lblGross" runat="server" Text='<%# Bind("PAYMENT_GROSS_LOCAL_CUST") %>' />
<asp:TextBox ID="txtGross" runat="server" />
<span id="spnGross" style="color:Red" ></span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
No comments:
Post a Comment