ASP.NET: validators and client code
TextBox1.Attributes["onblur"] = "for(i=0; i<document.all.length; i++){if (document.all[i].id.indexOf('Label1') > -1) { var element = document.all[i];element.innerHTML='Changes have been made';};}";
works like a charm and when focus leaves TextBox1, the text in the element Label1 changes to be "Hello".I then manipulated it a bit more and discovered that if in the html the Label1 element is set to visible="false", then the javascript fails due to the non-existance of that element. So, the final result is:The basic form:
When a required field is missing (note both the validator text and the change of text in the label caused by changing the focus):
When the two textboxes don't have identical content:
With asp code of:
<asp:Label ID="Label1" runat="server" Text="Label">Please make some changes</asp:Label>
<br />
<asp:TextBox ID="TextBox1" runat="server">TextBox1</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" Text="RequiredFieldValidator"></asp:RequiredFieldValidator>
<br />
<asp:TextBox ID="TextBox2" runat="server">TextBox2</asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="CompareValidator" ControlToCompare="TextBox2" ControlToValidate="TextBox1">CompareValidator</asp:CompareValidator><br/>
<asp:Button ID="Button1" runat="server" Text="Button" />
and a code behind of
protected void Page_Load(object sender, EventArgs e)
{
if (Label1.Visible)
{
TextBox1.Attributes["onblur"] = "for(i=0; i -1) { var element = document.all[i];element.innerHTML='Changes have been made';};}";
}
}