Josiane Ferice
Josiane Ferice

Reputation: 941

jQuery Validate: Form is Being Validated Twice

I have a form that I am using jQuery Validate to validate the form. I ran into an issue. The form is being validated twice. My variable 'isValid' is logged to the console twice after

The button that triggered the validation is a type button

<button type="button" name="submit" id="submit" vclass="btn btn-default">Submit</button>

Below is the JavaScript that has the rules and messages to be displayed to the user. It also include the click event for the button.

$(function () {
$("#createUser").validate({
    rules: {
        FirstName: "required",
        email: {
            required: true,
            minlength : 2
        },
        zipCode: {
            required: true,
            maxlength : 5
        },
        Telephone: {
            required: function (element) {
                return $('#options option:selected').val() != "1";
            },
            maxlength : 12
        },
        options: {
        required: true,
        notEqual: "1"
        }
    },
    messages: {
        FirstName: "Please specify your first name",
        email: {
            required: "This email field is required.",
            minlength : "email had to have a min length of 2."
        },
        zipCode: {
            required: "Zip code is a required field.",
            maxlength : "zip code cannot be more than 5 characters."
        },
        Telephone: {
            required : "Telephone is a required field.",
            maxlength : "Phone number has to have a max length of 12 characters."
        },
        options: {
            required: "Option is a required field",
            notEqual: "Select a different option"
        }
    }
  });

jQuery.validator.addMethod("notEqual", function (value, element, param) {
    return this.optional(element) || value != param;
}, "Please specify a different (non-default) value");


  $('#submit').on('click', function () {
     isValid = $('#createUser').valid();
     console.log(isValid);
  });
});

Most of the issues that I've found related to forms being validated twice have to do with having type submit instead button. Also, I a m not calling submit anywhere in my JavaScript file. What am I missing here?

Upvotes: 1

Views: 1392

Answers (1)

REDEVI_
REDEVI_

Reputation: 684

if the rules are in separate js , make sure the script is not referenced twice ,

Upvotes: 3

Related Questions