Validating jQuery with a type of "button" and not of type "submit" for a form

$ ('selector'). The validation () function seems to be more built for an input type button than a button type. How do I get it to work with a button type in a form?

@using(Html.BeginForm(new {id="TheForm"}))
{
     // code here
     <input id="TheButton" type="button">
}

JQuery

$(document).ready(function() {
      $("#TheForm").validate({
            rules: {
                 "AuditDoc.Title": {
                       required: true
                  }
            }
      });
      $("#TheButton").click(function() {

      });
});

Using this basic idea, how would I get jquery to check the operation of a button, not a submit type button? I saw examples where jQuery automatically displays error messages when rules are not executed using the send type, but it does not work with the button type. I would appreciate any advice!

+5
source share
4 answers
$(document).ready(function () {
        $("#TheForm").validate({
            rules: {
                "AuditDoc.Title": {
                    required: true
                }
            }
        });

        $("#TheButton").click(function () {
            if (!$("#TheForm").validate()) { // Not Valid
                return false;
            } else {
                $("#TheForm").submit()
            }
        });

        $('#btnReset').live('click', function (e) {
            //var $validateObj = $('#formId');
            //Or
            var $validateObj = $(this).parents('form');

            $validateObj.find("[data-valmsg-summary=true]").removeClass("validation-summary-errors").addClass("validation-summary-valid").find("ul").empty();
            $validateObj.find("[data-valmsg-replace]").removeClass("field-validation-error").addClass("field-validation-valid").empty();
        });
    });
+4
source
$(document).ready(function(){

     $("#myform").validate();
     $("#byuttion").click(function() {

       if($("#myform").valid())
       {
          $("#myform").submit();
       }
       else 
      {
          return false;
      }

      });

});
+5
source

:

$("#TheButton").click(function() {
     $("#TheForm").submit()
});
+4

It works almost the same as with regular input, except that it does not support the submit method. preventDefault simply stops the default page execution.

$("#TheButton").click(function(event) {
    event.preventDefault(); //or return false
    getData();
});
+2
source

All Articles