Firstly, and most importantly, you must wrap your input elements inside <form></form> tags for the jQuery Validate plugin to operate. However, a submit button is not required.

Secondly, you can programatically trigger the validity test of any or all elements without a submit button by using the .valid() method.

Copy$(document).ready(function() {

    $('#myform').validate({  // initialize the plugin on your form.
        // rules, options, and/or callback functions
    });

    // trigger validity test of any element using the .valid() method.
    $('#myelement').valid();

    // trigger validity test of the entire form using the .valid() method.
    $('#myform').valid();

    // the .valid() method also returns a boolean...
    if ($('#myform').valid()) {
        // something to do if form is valid
    }

});

DEMO: http://jsfiddle.net/URQGG/

Answer from Sparky on Stack Overflow
๐ŸŒ
Jqueryvalidation
jqueryvalidation.org โ€บ documentation
Documentation | jQuery Validation Plugin
A single line of jQuery to select the form and apply the validation plugin, plus a few annotations on each element to specify the validation rules. Of course that isn't the only way to specify rules. You also don't have to rely on those default messages, but they come in handy when starting to setup validation for a form. After trying to submit an invalid form, the first invalid element is focused, allowing the user to correct the field.
Discussions

jQuery Validation for all inputs of a given type
I'm trying to write a JavaScript function that would cover the validation of all the input elements of the type="time". I am looking at the documentation of jQuery Validate but I'm only finding exa... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Validate form input fields using jquery - javascript
@BhumiShah I don't want to use Id's I want the form to validate all the text input fields all at once regardless of how many they are. ... gone are the days to put js code for validation .. u can try this library itself - jquery form validator - formvalidator.net . More on stackoverflow.com
๐ŸŒ stackoverflow.com
Validate all the inputs of a table in jquery
Hi, it loops through the inputs ... of input fields (firstname, lastname, recipient_number, email will be looped through four times if there is 1 item in the array, 8 times if there are two, etc) 2016-09-14T08:09:27.233Z+00:00 ... In your question you mentioned that I am trying to look for a way using JQuery to loop through all the generated ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to check if all inputs are not empty with jQuery
I need to validate a form with jQuery. I can check all my inputs one by one, but it's not a very practical solution. How can i check if all my inputs are non-empty more efficiently? In my form i can have input elements of different types: text, several groups of radio, select etc. ... write same class for all the fields ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Formden
formden.com โ€บ blog โ€บ validate-contact-form-jquery
How to Validate Form Fields Using jQuery | Formden.com
March 19, 2014 - Validate each field as it is being entered. Valid inputs will turn green while invalid inputs will turn red. When the submit button is pushed, jQuery will check whether all fields are valid.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ form-validation-using-jquery
Form Validation using jQuery | GeeksforGeeks
September 16, 2024 - Perform the validation task for all the input fields such as username, email, password, and confirm password. Example 1: This example illustrates the simple form validation using jQuery.
๐ŸŒ
Jqueryvalidation
jqueryvalidation.org โ€บ reference
Reference documentation | jQuery Validation Plugin
For example, if you're writing ... a single field, you could call the existing email method for each email: An error message displays a hint for the user about invalid elements, and what is wrong. There are four ways to provide error messages. Via the title attribute of the input element to validate, via data attributes, via error labels and via plugin settings (option messages). All validation ...
๐ŸŒ
SitePoint
sitepoint.com โ€บ blog โ€บ javascript โ€บ how to set up basic jquery form validation in two minutes
jQuery Form Validation Tutorial: Simple Example with jQuery Validation Plugin | SitePoint
January 6, 2025 - To style the form, create a new CSS file and include it in the head section of your HTML code. Then, initialize the jQuery validation on form submit in a new JavaScript(JS) file, specifying each input fieldโ€™s validation rules and error messages.
๐ŸŒ
Jqueryvalidation
jqueryvalidation.org โ€บ validate
.validate() | jQuery Validation Plugin
May 23, 2013 - Key/value pairs, where the key ... an input field, values the message to be displayed for that input. ... An array for all currently validated elements. Contains objects with the following two properties: ... The message to be displayed for an input. ... The DOMElement for this entry. ยถerrorPlacement (default: Places the error label after the invalid element) ... Customize placement of created error labels. First argument: The created error label as a jQuery ...
Find elsewhere
๐ŸŒ
Developer Diary
varunver.wordpress.com โ€บ 2014 โ€บ 03 โ€บ 07 โ€บ jquery-validate-all-inputs-within-a-div-incomplete
jQuery โ€“ Validate all inputs within a DIV (Incomplete)
March 7, 2014 - //-- Gets called on form submit function validateForm() { var arr_evalFieldNames = ["eval|company", "eval|first_name", "eval|last_name", "eval|email"]; if (!validateMandatoryElements(arr_evalFieldNames)) { return false; } } //-- Accept array of field names and return false if either one of them is blank function validateMandatoryElements(arr_fieldNames) { for (index = 0; index < arr_fieldNames.length; ++index) { $element = $( '[name="'+arr_fieldNames[index]+'"]' ); if ($element.val().trim() == "") { alert("Enter valid valid value for Eval data"); $element.focus(); return false; } } return true; }
๐ŸŒ
Formspree
formspree.io โ€บ blog โ€บ jquery-validate
Mastering Form Validation with jQuery Validate | Formspree
January 13, 2025 - Hereโ€™s what the form will look ... error messages will look like: At this point, if you edit the contents of the field, jQuery Validate will automatically take care of running the validations on each change and finally once ...
Top answer
1 of 2
11

Here is the working code:

https://jsfiddle.net/bhumi/o2gxgz9r/47570/

I have changed selector to use id

You need to use loop in handle error:

var Validator = function(form) {

    this.form = $(form);

    var Elements = {
        name: {
            selector: $('input[type=text]'),
            reg: /^[a-zA-Z]{2,20}$/
        },

        email: {
            selector: $('input[type=email]'),
            reg: /^[a-z-0-9_+.-]+\@([a-z0-9-]+\.)+[a-z0-9]{2,7}$/i
        },

        message: {
            selector: $('textarea'),
            reg: /^\s+$/
        }
    };

    var handleError = function(element, message, v1) {
        if (v1.selector.length > 1) {
            var ss = v1.selector;

            $(ss).each(function(i, v) {
            $(v).removeClass('input-error');
            if($(v).val() == ''){
              $(v).addClass('input-error');
              var $err_msg = $(v).parent('div');
              if($(v).parent('div').find('.error').length == 0) {
                    var error = $('<div class="error"></div>').text(message);
               }else{
                    $(v).parent('div').find('.error').text('');
                    var error = $(v).parent('div').find('.error').text(message);
                    $(this).siblings('.error').show();
               }
               error.appendTo($err_msg);
             }else{
               $(v).siblings('.error').text('')
             }
             $(v).keyup(function() {
                 $(error).fadeOut(1000, function() {
                     element.removeClass('input-error');
                });
             });
          });
        } else {
            element.addClass('input-error');
            var $err_msg = element.parent('div');
            if(element.parent('div').find('.error').length == 0) {
                  var error = $('<div class="error"></div>').text(message);
             }else{
                  element.parent('div').find('.error').text('');
                  var error = element.parent('div').find('.error').text(message);
                  $(this).siblings('.error').show();
             }
            error.appendTo($err_msg);
            element.keyup(function() {
                $(error).fadeOut(1000, function() {
                    element.removeClass('input-error');
                });
            });
        }

    };

    this.validate = function() {

        this.form.submit(function(e) {

            for (var i in Elements) {

                var type = i;
                var validation = Elements[i];
                switch (type) {
                    case 'name':
                        if (!validation.reg.test(validation.selector.val())) {
                            handleError(validation.selector, 'Not a valid name.', validation);
                        }
                        break;
                    case 'email':
                        if (!validation.reg.test(validation.selector.val())) {
                            handleError(validation.selector, 'Not a valid e-mail address.', validation);
                        }
                        break;
                    case 'message':
                        if (validation.reg.test(validation.selector.val()) || validation.selector.val() == '') {
                        handleError(validation.selector, 'Message field cannot be empty.', validation);
                        }
                        break;
                    default:
                        break;


                }

            }

            e.preventDefault();
        });

    };
};

var validator = new Validator('#test');
validator.validate();
2 of 2
1

I hope this is what you were trying to achieve. This took longer than expected but I tried to achieve it. This whole form is custom form. You could have used the existing plugins to achieve it. Any which ways it took much time to figure it out. Consider the question as most of things are working, ignore if something's not what you want.

$(document).ready(function() {

  /* contact form validation */
  var Validator = function(formObject) {
    this.form = $(formObject);
    var Elements = {
      name: {
        reg: /^[a-zA-Z ]{2,20}$/,
        require: true,
        error: "Not a valid name.",
      },

      email: {
        reg: /^[a-z-0-9_+.-]+\@([a-z0-9-]+\.)+[a-z0-9]{2,7}$/i,
        error: "Not a valid e-mail address.",
      },
      phone: {
        reg: /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/,
        error: "Not a valid number.",
      },

      message: {
        reg: /^(?!\s*$).+/,
        error: "Message field cannot be empty.",
      },
      gender: {
        error: "gender is required",
      },
      selectOption: {
        error: "this field is required",
        required: true
      }
    };

    var handleError = function(element, message) {
      element.addClass('input-error');
      var $err_msg = element.parent('div');
      $err_msg.find('.error').remove();

      var error = $('<div class="error"></div>').text(message);
      error.appendTo($err_msg);
      console.log(element);


      element.on('keypress change', function() {
        $(error).fadeOut(1000, function() {
          console.log(element);
          element.removeClass('input-error');
        });
      });

    };

    /* Select Option */

    this.validate = function() {
      var errorCount = 0;

      this.form.find("select").each(function(index, field) {
        var type = $(field).data("validation");
        var validation = Elements[type];
        if ($(field).val() == "") {
          errorCount++;
          handleError($(field), validation.error);
        }
      });

      this.form.find("input, textarea").each(function(index, field) {
        var type = $(field).data("validation");
        var validation = Elements[type];
        if (validation !== undefined) {
          var re = new RegExp(validation.reg);
          if (validation) {
            if (!re.test($(field).val())) {
              errorCount++;
              handleError($(field), validation.error);
            }
          }
        }
      })

      /* Radio button */

      var radioList = $('input:radio');
      var radioNameList = new Array();
      var radioUniqueNameList = new Array();
      var notCompleted = 0;
      for (var i = 0; i < radioList.length; i++) {
        radioNameList.push(radioList[i].name);
      }
      radioUniqueNameList = jQuery.unique(radioNameList);
      console.log(radioUniqueNameList);
      for (var i = 0; i < radioUniqueNameList.length; i++) {
        var field = $('#' + radioUniqueNameList[i]);
        var type = field.data("validation");
        var validation = Elements[type];
        if ($('input[name=' + type + ']:checked', '#test').val() == undefined) {
          errorCount++;
          handleError($(field), validation.error);
        }
      }

      return errorCount == 0;
    };
  };

  /* Submit form*/

  $(function() {

    $("form#test").on('submit', function(e) {
      var NoErrors = new Validator(this).validate();
      if (NoErrors == true) {
        $.ajax({
          url: this.action,
          type: this.method,
          data: $(this).serialize(),
          success: function() {
            // AJAX request finished, handle the results and error msg
            $('.success_msg').fadeIn().delay(3000).fadeOut();
            $('input[type!="submit"], textarea').val('').removeClass('error');
          }
        });

      }
      return false;
    })

  })

});
body {
  background: #fff;
  color: #333;
  font: 76% Verdana, sans-serif;
}

form {
  margin: 1em 0 0 2em;
  width: 90%;
}

fieldset {
  margin: 0;
  border: 1px solid #ccc;
  padding-bottom: 1em;
}

legend {
  font-weight: bold;
  text-transform: uppercase;
}

label {
  float: left;
  width: 5em;
  padding-right: 2em;
  font-weight: bold;
}

div {
  margin-bottom: 30px;
}

input {
  font: 1em Verdana, sans-serif;
}

fieldset ul li input {
  float: left;
  width: 120px;
  border: 1px solid #ccc;
}

textarea {
  width: 300px;
  height: 200px;
  border: 1px solid #ccc;
  font: 1em Verdana, sans-serif;
}

form p {
  margin: 0;
  padding: 0.4em 0 0 7em;
}

form p input {
  background: #666;
  color: #fff;
  font-weight: bold;
}

div.error {
  clear: left;
  margin-left: 5.3em;
  color: red;
  padding-right: 1.3em;
  height: 100%;
  padding-bottom: 0.3em;
  line-height: 1.3;
}

.input-error {
  background: #ff9;
  border: 1px solid red;
}

.success_msg {
  width: 350px;
  line-height: 40px;
  border: 1px solid green;
  border-radius: 5px;
  background-color: rgba(213, 255, 187, 0.7);
  display: none;
  position: absolute;
  bottom: 5px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 999;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<form action="#" method="post" id="test">
  <fieldset>

    <legend>Contact information</legend>


    <div>
      <label for="firstname">Firstname:</label>
      <input type="text" name="firstname" id="firstname" data-validation="name" />
    </div>


    <div>
      <label for="lastname">Lastname:</label>
      <input type="text" name="lastname" id="lastname" data-validation="name" />
    </div>

    <div>
      <label for="email">Email:</label>
      <input type="email" name="email" id="email" data-validation="email" />

    </div>
    <div>
      <label for="phone">phone</label>
      <input type="number" name="phone" id="phone" data-validation="phone" />
    </div>

    <div>
      <label>Gender:</label>
      <input type="radio" name="gender" value="male" data-validation="gender" />
      <input type="radio" name="gender" value="female" data-validation="gender">
    </div>

    <select data-validation="selectOption">
      <option value="">Select any option</option>
      <option value="red">Red</option>
      <option value="blue">Blue</option>
      <option value="green">Green</option>
    </select>

    <div>
      <label for="message">Message:</label>
      <textarea id="message" name="message" cols="30" rows="15" data-validation="message"></textarea>
    </div>

    <p><input type="submit" name="send" id="send" value="Send" /></p>

  </fieldset>
  <div class="success_msg">
    <p>Form submitted Successfully</p>
  </div>
</form>

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 39484786 โ€บ validate-all-the-inputs-of-a-table-in-jquery
Validate all the inputs of a table in jquery
This will allow you to validate the fields which are empty, and try to use the filter, that helps you to do it without any loop. ... $('#sender_container').on('submit',function(e) { e.preventDefault(); validate(); }); function validate() { $('#sender_container > input[type="text"]') .removeClass('error') .filter(function() { // Remove error classes. Filter return !$.trim(this.value); }) .addClass('error'); } ... <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form method="post" id="sender_container"> <input type="text" name="name[]" /> <input type="text" name="name[]" /> <input type="text" name="name[]" /> <input type="text" name="name[]" /> <input type="text" name="name[]" /> <input type="submit" name="submit" /> </form>
๐ŸŒ
HTML Form Guide
html.form.guide โ€บ jquery โ€บ validation-using-jquery-examples
Form Validation Using Jquery Examples | HTML Form Guide
In the previous example, we wrote code that validated the forms data from scratch. To make this process faster and easier, you can use a library like the jQuery Validation Plugin. With this, you only have to specify a few rules for each field of your form that is to be validated, and the plugin ...
๐ŸŒ
The Web Fosters
thewebfosters.com โ€บ home โ€บ blogs โ€บ how to validate dynamically generated input fields with jquery validation plugin ??
How to validate dynamically generated input fields with jQuery Validation plugin ?? - The Web Fosters
May 28, 2017 - As we know jQuery validation plugin is the most commonly used client side form validation library which provides plenty of customization with an ease to save time for manually validating each HTML form elements from scratch. Though this plugin provides lots of inbuilt function to customize ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-validate-a-form-using-jQuery
How to validate a form using jQuery?
September 9, 2023 - jQuery(document).ready(function() { jQuery("#forms).validate({ rules: { firstname: 'required', lastname: 'required', u_email: { required: true, email: true,//add an email rule that will ensure the value entered is valid email id. maxlength: 255, }, } }); }); ... messages: { firstname: 'This ...
๐ŸŒ
SoloDev
solodev.com โ€บ blog โ€บ web-design โ€บ how-to-use-jquery-validator-for-your-form-validation.stml
How to Use jQuery Validator for Your Form Validation
October 14, 2019 - In this tutorial, we are using Bootstrap and the good thing about using the jQuery validation plugin is we don't have to change out mark-up to make the plugin work. In our markup, all we need to do is add an id to our form tag and all the input fields we have such as "first name", "last name", ...
๐ŸŒ
Medium
krnk97.medium.com โ€บ how-to-add-validations-to-an-html-form-or-inputs-using-jquery-in-2022-jquery-form-validation-fccac43f0f75
How to add validations to an html form or Inputs using jQuery in 2022 | jQuery Form Validation Methods with Repo link! | by Karan Kaul | ใ‚ซใƒฉใƒณ | Medium
March 5, 2022 - I have not added a check to see if user has entered inputs other than digits in this field, but that can also be checked easily or you can just use a number input field on the form. If you would like to add a check then you can do a check like the one I have written below. parseInt(x) will return only integers from the input and hence it wonโ€™t match x if there were characters in it as well. ... Username is 4 or more characters long. Email contains โ€œ@โ€ symbol and โ€œ.comโ€ is at the end. Phone number contains 10 digits. I think at this point, it must be quite clear to you how we can do validations and how easy they are to implement.
๐ŸŒ
Tech Solution Stuff
techsolutionstuff.com โ€บ post โ€บ how-to-dynamically-validate-all-fields-in-jquery
How to Dynamically Validate All Fields in jQuery
January 25, 2025 - <!-- Bootstrap JS and Popper.js --> <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script> <!-- jQuery --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script> <script> $(document).ready(function () { $('#myForm').submit(function (event) { event.preventDefault(); // Prevent form submission to perform validation var isValid = true; // Flag for overall validation var errorMessages = []; // Array to store error message