按Tab键时如何验证表单字段?

时间:2015-11-22 19:31:18

标签: javascript jquery jquery-validate

当您按 TAB 键时,如何查看/有效表单上的每个字段。目前只有在您开始在框中输入时它才有效。有fiddle.js

我使用validatejs脚本使用以下选项:

$('#form').validate({ // initialize the plugin
    success: function(label) {
        label.addClass("valid");
    },
    rules: {
        formFName: {
            required: true,
            minlength: 2
        },
        formLName: {
            required: true,
            minlength: 2
        },
        formEmail: {
            required: true,
            email: true
        }
    },
    messages: {
        formFName: {
            required: "Enter your first name, please.",
            minlength: "A name usually has at least 2 characters, right?"
        },
        formLName: {
            required: "Enter your last name, please.",
            minlength: "A name usually has at least 2 characters, right?"
        },
        formEmail: {
            required: "Enter your email address, please.",
            email: "Enter an existing email address, please."
        }
    },
    submitHandler: function (form) {
        alert('sent');
    }
});

1 个答案:

答案 0 :(得分:2)

你可以这样做:

$( '#form' ).validate( {
    onkeyup: false,
    onfocusout: function( element, event ) {
        this.element( element );
    }
} );

编辑1:

或者如果您真的只想使用TAB键作为验证器,您可以这样做:

$( '#form' ).validate( {
    onkeyup: function ( element, event ) {
        if ( event.which === 9 ) {
            this.element(element);
        } else {
            return;
        }
    },
} );