Elegant way to combine ASP.NET validation with JQuery

前端 未结 3 1286
误落风尘
误落风尘 2021-02-02 18:41

How can I best combine JQuery with ASP.NET client side validation model?

I\'ve generally avoided implementing ASP.NET validation model because it always seems overkill f

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-02 18:47

    ASP.NET has many validation controls, one of them is the CustomValidator.
    You can give it a custom JavaScript function that uses jQuery and returns true or false via argument. You can use this control to display the error message automatically, or just run the jQuery code and handle the display manually.

    Aspx:

    
    
    

    JavaScript:

    function checkTextareaLengths(source, args){
      args.IsValid = true;
      $('textarea').each(function(){
        if($(this).text().lenght > 400)
          args.IsValid = false;
      });
    }
    

提交回复
热议问题