javascriptasp.netrequiredfieldvalidatorrangevalidator

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" />

Solution

  • 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>