Hi I have some jquery code that checks to see if an asp validator has style=”display:inline”
If it does I add an error class to the errors parent which is a div.
But when i have two validators this doesnt work. why is that? here is the code
////////// Add Error Class ////////////////
addErrorClass: function (obj) {
if ($(obj).parent().find('.errorMessage').parent().css("display") == "inline") {
$(obj).parent().addClass("error");
} else {
$(obj).parent().removeClass("error");
}
},
////////// Add Error Class /////////////
////////// Validate Form Fields /////////////
ValidateFF: function () {
$('.required input').blur(function () {
project.addErrorClass(this);
if ($(".modal").length) {
Spacedadi_UI.AdjustRadWindow();
};
});
$('#btnSave').click(function () {
$('.required input').each(function (index) {
// Force ASP Validation before click event when using link buttons
var cErrorID = $(this).parent().find("span").attr('id');
ValidatorEnable(document.getElementById(cErrorID), true);
project.addErrorClass(this);
if ($(".modal").length) {
project.AdjustRadWindow();
};
}); //end of click
if ($(".modal").length) {
project.AdjustRadWindow();
};
}); // end validate function
},
////////// Validate Form Fields /////////////
HTML Code
<div class="field required"><asp:Label ID="lbEmail" runat="server" CssClass="label" Text="Email" AssociatedControlID="tbEmail"></asp:Label>
<asp:TextBox ID="tbEmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvEmail" runat="server" errorMessage="<span class='errorMessage'>Please enter an email address</span>"
ControlToValidate="tbEmail" CssClass="" ValidationGroup="ValAddContact"
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revtbEmail" runat="server" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ControlToValidate="tbEmail" errorMessage="<span class='errorMessage'>Invalid Email Format</span>" ValidationGroup="ValAddContact"
CssClass=""></asp:RegularExpressionValidator>
</div>
Thanks
I’m not really sure about ASP.NET, but have you tried inspecting the HTML on the page to see if it’s renamed anything you’re relying on? I seem to remember ASP renaming IDs and Names of elements to ensure they’re unique.
You could also try using .hasClass(‘errorMessage’) instead of find(…).
Does the button click event actually fire in the first place?