javascript - jquery validator onfocusout not working for checkbox and radio - Stack Overflow

Using jquery validator. validation is not performed when using tab to navigate the form.Here's the

Using jquery validator. validation is not performed when using tab to navigate the form.

Here's the fiddle

$(function () {
         var validator = $("#myForm").validate({ 
             onfocusout: function(element) {
                   this.element(element);
                },
             rules: { 
                 fname: "required", 
                 check: "required",
                 color: "required"
                }, 
             messages: { 
                 fname: "Enter your firstname", 
                 check: "you know you do",
                 color: "pick one!",
             }, 
         }); 
     });

I tried to perform a on blur on the checkbox. However, the event is trigger on form load. Here's the improved fiddle

$('#check').on('blur', function() {
            $("#myForm").validate().element( this );
        }).blur();

Using jquery validator. validation is not performed when using tab to navigate the form.

Here's the fiddle

$(function () {
         var validator = $("#myForm").validate({ 
             onfocusout: function(element) {
                   this.element(element);
                },
             rules: { 
                 fname: "required", 
                 check: "required",
                 color: "required"
                }, 
             messages: { 
                 fname: "Enter your firstname", 
                 check: "you know you do",
                 color: "pick one!",
             }, 
         }); 
     });

I tried to perform a on blur on the checkbox. However, the event is trigger on form load. Here's the improved fiddle

$('#check').on('blur', function() {
            $("#myForm").validate().element( this );
        }).blur();
Share Improve this question edited Jun 1, 2013 at 16:38 user1549804 asked May 29, 2013 at 9:23 user1549804user1549804 1311 silver badge10 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

The solution/work around the onfocusout issue is to use the on blur for both checkbox and radio.

$(function () {
    var validator = $("#myForm").validate({ 
        onfocusout: function(element) {
            this.element(element);
    },
    rules: { 
        fname: "required", 
        check: "required",
        radio: "required",
        color: "required"
    }, 
    messages: { 
        fname: "Enter your firstname", 
        check: "check your checkbox",
        radio: "check your radio",
        color: "pick one!",
    }, 
    }); 

    $('#check').on('blur', function() {
        $("#myForm").validate().element( this );
    });
    $('#radio').on('blur', function() {
        $("#myForm").validate().element( this );
    });
});

Take note that you do not call the blur function like this:

$('#check').on('blur', function() {
    $("#myForm").validate().element( this );
}).blur();

the initial blur function will cause the validation to be trigger on load

Here's the working fiddle

onfocusout does not show error message, otherwise the best way to validate elements on blur is;

onfocusout: function(element) { 
    if( $(element).attr('name') ) { 
            this.element(element);  
    }
}

Check the onfocusout option.

"Validate elements (except checkboxes/radio buttons) on blur. If nothing is entered, all rules are skipped, except when the field was already marked as invalid."

Validate

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744375752a4571178.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信