ASP.NET client side validation on blur

I have few textboxes and asp.net validation controls on a page. The validation fires when I click on “Submit” button. Is there anyway to fire the validation as soon as I leave the textbox? I would like to do the same validation for other 20+ fields in the form. However, only validating specific textbox when leaving it.

        <asp:TextBox ID="txtclass" runat="server"></asp:TextBox>
        <asp:RangeValidator ID="rvclass" 
           runat="server" ControlToValidate="txtclass" 
           ErrorMessage="Enter your class (6 - 12)" MaximumValue="12" 
           MinimumValue="6" Type="Integer">
        </asp:RangeValidator>
        <asp:Button ID="btnsubmit" runat="server" onclick="btnSubmit_Click" Text="Submit" />

Answer

Create a js function to perform the validating, then for each textbox you want to validate set the onblur and call that function. (I’m just free writing below so excuse the syntax error but you should get the gist)

  function validateClass(val)
  {
    if(val.value > 5 && val.value < 13)
      {
          //alert(good!)
      }
     else
     {
          //alert(show error?)
     }
  
  }

call it in your textbox

      <asp:TextBox ID="txtclass" runat="server" onblur="validateClass(this);"></asp:TextBox>
Source: stackoverflow
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .