Jquery: How do I check if there are errors?

后端 未结 3 1109
遥遥无期
遥遥无期 2020-12-20 00:31

I am trying to bassistance plugin for my form validation, now my form consists of several radio button groups, some of them are \"required\".

Now,

  1. how
3条回答
  •  难免孤独
    2020-12-20 00:50

    Are you asking how you can use the plugin? It's pretty well-documented, but anyway these snippets should assist. Here I take a simple form with two lots of radio-button groups, validate them using the plugin. The main things to note in the validation code are that we have two handler callbacks (for 'invalid' and 'success' states) plus a set of validation rules to apply.

    First, the example form:

    Option 1 Option 2 Option 3

    Option 1 Option 2 Option 3

    … and second, the validation code:

    $(document).ready(function(){
        $("#your-form").validate({
            invalidHandler: function(form, validator){
                var errors = validator.numberOfInvalids();
                var message = (errors == 1) ? "One of your groups won't validate." : "Neither of your groups will validate.";
                alert(message);
            },
            submitHandler: function(){
                alert("Everything's OK, both radio button groups are set.");
                $('#your-form').ajaxSubmit()
            },
            rules: {group_one: {required: true}, group_two: {required: true}}
        });
    });
    

    Does that help?

提交回复
热议问题