jquery validate - validating a field on pageload

前端 未结 2 761
小鲜肉
小鲜肉 2021-01-26 11:51

I\'ve got an input field on my plain html form that I would like to be marked as invalid (and the error message shown) from the moment the page loads.

Is this possible t

2条回答
  •  轮回少年
    2021-01-26 11:55

    Managed to come up with a solution. It involved adding a 'dummy' validation element that is shown when the page is loaded. As soon as the user inputs a value into the corresponding field, the dummy element is hidden and the real one is shown.

    I used the highlight and unhighlight options for the validate method to achieve what i wanted.

    $("#myform").validate({
        highlight: function(element){
            if($(element).hasClass("math")){
               $(".temp").hide();
            }
        },
    
        unhighlight: function(element){
            if($(element).hasClass("math")){
               $(".temp").hide();
            }
        }
    });
    

    Maybe this will help someone else out in future.

提交回复
热议问题