jQuery validation trigger error message

后端 未结 1 1353
被撕碎了的回忆
被撕碎了的回忆 2021-02-03 23:02

I have a kinda wierd problem. I want to trigger a jquery-valdation errormessage on an element even thou it\'s valid.

Scenario: I have a large form. One of the inputs is

1条回答
  •  走了就别回头了
    2021-02-03 23:58

    Is that possible with jQuery Validate or do i need to create my own function?

    Both, sort of. You can use the showErrors function to display errors on the form manually, but since you don't want this to be a rule that is enabled when you submit the form, you'll have to do your own checking for the value in the input.

    Something like:

    var $validator = $("#myform").validate();
    $("#checkid").click(function() {
        var errors;
    
        if (!$("#personalid").val()) {
            /* Build up errors object, name of input and error message: */
            errors = { personalid: "Please enter an ID to check" };
            /* Show errors on the form */
            $validator.showErrors(errors);            
        }        
    });
    

    Example: http://jsfiddle.net/andrewwhitaker/XjZer/

    Note that the form will still submit if there is nothing in the personalid field.

    0 讨论(0)
提交回复
热议问题