I have a TextBox input element that has a RequiredFieldValidator as such:
<div>
<label>First name</label>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorFirstname" runat="server" ErrorMessage="Required" ControlToValidate="TextBoxFirstname"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBoxFirstname" runat="server"></asp:TextBox>
</div>
When the TextBox is empty on submit I want to add the class ‘form-error’ to the parent Div:
<div class="form-error">
<label>First name</label>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorFirstname" runat="server" ErrorMessage="Required" ControlToValidate="TextBoxFirstname"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBoxFirstname" runat="server"></asp:TextBox>
</div>
Is this possible, and if so – how do I do it?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Use the following:
<script>
$(function(){
if (typeof ValidatorUpdateDisplay != 'undefined') {
var originalValidatorUpdateDisplay = ValidatorUpdateDisplay;
ValidatorUpdateDisplay = function (val) {
if (!val.isvalid) {
$("#" + val.controltovalidate).closest("div").addClass("form-error");
}
originalValidatorUpdateDisplay(val);
}
}
});
</script>
This code decorates the original ValidatorUpdateDisplay function responsible for updating the display of your validators, updating the controltovalidate closest div.
Method 2
with RequiredFieldValidator you can not add your custom code.
you should use asp.net customvalidator control, and write your own custom validation javascript function which sets the class to the div.
Method 3
I think you may find the below option useful. Basically it overrides one of ASP.NET validations default scripts. And shoves some logic into it to add / remove error class on parent div.
The problem I had faced with this task was getting my script to execute after validations were triggered since the DOM is not reloaded. By overriding this the original validation script you have the ability to run your own script when the validation display is updated.
This way you can continue to use your other validation controls. You do have to place the script at the bottom of your page or masterpage. I also used JQuery to make things simpler.
*Note the below code requires your validators be set to display dynamic. If you don’t want them to be visible you can add a css class to them and set display: none.
<script type="text/javascript">
function ValidatorUpdateDisplay(val) {
if (typeof (val.display) == "string") {
if (val.display == "None") {
return;
}
if (val.display == "Dynamic") {
val.style.display = val.isvalid ? "none" : "inline";
return;
}
}
if ((navigator.userAgent.indexOf("Mac") > -1) && (navigator.userAgent.indexOf("MSIE") > -1)) {
val.style.display = "inline";
}
val.style.visibility = val.isvalid ? "hidden" : "visible";
//--- Add / Remove Error Class
var triggeredSiblings = 0;
if (val.isvalid) {
$(val).siblings('span').each(function () {
if ($(this).isvalid == false) { triggeredSiblings = 1; }
});
if (triggeredSiblings == 0) { $(val).closest("div").removeClass("error"); }
} else {
$(val).closest("div").addClass("error");
}
}
</script>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0