6

$('selector').validation() seems to be built more for the "input" type button than the "button" type. How would I get it working with a button type within the 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 validate working with a button rather than a submit type button? I've seen examples where JQuery automatically displays error messages when the rules aren't met using submit type, but it doesn't appear to work with button type. I'd appreciate any advice!

1
  • use this one button click $("#TheForm").validate(); Commented Aug 11, 2012 at 13:05

4 Answers 4

8
$(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();
        });
    });
Sign up to request clarification or add additional context in comments.

3 Comments

just submit the form - validate does the rest.
The problem is that validate doesn't automatically work with type "button" the way it does with type "submit". You don't get all that "validate does the rest" business with plain old buttons. It seems to be built for "submit" type buttons.
In my version I need to first call $("#TheForm").validate(); and then use the valid method: if(!$("#TheForm").valid()) { }
7
$(document).ready(function(){

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

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

      });

});

1 Comment

Thanks mate. We will need to validate the form before and then check. Problem solved.
4

One hackish way could be:

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

4 Comments

That's not even hackish - the point is that you want to submit the form... jQuery validate does the rest. I would call most of the other answers hacky - doing the work validate would have done anyways :)
@TheDude - Look at what he's actually doing - he's making the click of the button submit the form. It is emulating what a submit button does very simply.
@Ryley - What it comes down to is that $.validation is built for SUBMIT type buttons, not BUTTON type. When I try using the BUTTON type, the .validation doesn't kick in. I tried using it for SUBMIT type buttons and it works as intended. Unfortunately, our application needs to be able to use BUTTON type.
Ryley is correct, all you have to do is get the form to submit on click, and then it works just like a type=submit. If the form is invalid, the form submittal is cancelled.
3

It works pretty much the same as it would with a normal input except it doesn't support the submit method. preventDefault just stops the page from doing it's default request.

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

1 Comment

I'll give this a try... though giving an upvote, believing it will work :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.