You can simply use the jQuery Validate plugin as follows.

jQuery:

$(document).ready(function () {

    $('#myform').validate({ // initialize the plugin
        rules: {
            field1: {
                required: true,
                email: true
            },
            field2: {
                required: true,
                minlength: 5
            }
        }
    });

});

HTML:

<form id="myform">
    <input type="text" name="field1" />
    <input type="text" name="field2" />
    <input type="submit" />
</form>

DEMO: http://jsfiddle.net/xs5vrrso/

Options: http://jqueryvalidation.org/validate

Methods: http://jqueryvalidation.org/category/plugin/

Standard Rules: http://jqueryvalidation.org/category/methods/

Optional Rules available with the additional-methods.js file:

maxWords
minWords
rangeWords
letterswithbasicpunc
alphanumeric
lettersonly
nowhitespace
ziprange
zipcodeUS
integer
vinUS
dateITA
dateNL
time
time12h
phoneUS
phoneUK
mobileUK
phonesUK
postcodeUK
strippedminlength
email2 (optional TLD)
url2 (optional TLD)
creditcardtypes
ipv4
ipv6
pattern
require_from_group
skip_or_fill_minimum
accept
extension
Answer from Sparky on Stack Overflow
Top answer
1 of 2
324

You can simply use the jQuery Validate plugin as follows.

jQuery:

$(document).ready(function () {

    $('#myform').validate({ // initialize the plugin
        rules: {
            field1: {
                required: true,
                email: true
            },
            field2: {
                required: true,
                minlength: 5
            }
        }
    });

});

HTML:

<form id="myform">
    <input type="text" name="field1" />
    <input type="text" name="field2" />
    <input type="submit" />
</form>

DEMO: http://jsfiddle.net/xs5vrrso/

Options: http://jqueryvalidation.org/validate

Methods: http://jqueryvalidation.org/category/plugin/

Standard Rules: http://jqueryvalidation.org/category/methods/

Optional Rules available with the additional-methods.js file:

maxWords
minWords
rangeWords
letterswithbasicpunc
alphanumeric
lettersonly
nowhitespace
ziprange
zipcodeUS
integer
vinUS
dateITA
dateNL
time
time12h
phoneUS
phoneUK
mobileUK
phonesUK
postcodeUK
strippedminlength
email2 (optional TLD)
url2 (optional TLD)
creditcardtypes
ipv4
ipv6
pattern
require_from_group
skip_or_fill_minimum
accept
extension
2 of 2
19

you can use jquery validator for that but you need to add jquery.validate.js and jquery.form.js file for that. after including validator file define your validation something like this.

<script type="text/javascript">
$(document).ready(function(){
    $("#formID").validate({
    rules :{
        "data[User][name]" : {
            required : true
        }
    },
    messages :{
        "data[User][name]" : {
            required : 'Enter username'
        }
    }
    });
});
</script>

You can see required : true same there is many more property like for email you can define email : true for number number : true

🌐
Jqueryvalidation
jqueryvalidation.org › files › demo
jQuery validation plug-in - main demo
jQuery Mobile Form Validation · Displaying Errors within Field Labels · Loading via RequireJS · Using with Bootstrap · Using with Semantic-UI · Retro Cinema Registration · Remember The Milk signup form · Marketo signup form · Buy and Sell a House multipart form ·
🌐
Jqueryvalidation
jqueryvalidation.org › validate
.validate() | jQuery Validation Plugin
May 23, 2013 - Example: Validates the name-field as required and having at least two characters. Provides a callback message using jQuery.validator.format to avoid having to specify the parameter in two places.
🌐
Formspree
formspree.io › blog › jquery-validate
Mastering Form Validation with jQuery Validate | Formspree
January 13, 2025 - For example, instead of saying “Invalid input format,” specify the issue: “Please enter a valid email address.” Customizing error messages in jQuery validation is straightforward using the messages property, allowing you to create specific ...
🌐
GeeksforGeeks
geeksforgeeks.org › jquery › form-validation-using-jquery
Form Validation using jQuery - GeeksforGeeks
July 15, 2025 - At the bottom of the <body> tag, ... having jQuery code for form validation. Create an app.js file that validates the whole form as given below in the code. In the app.js file, when the document is ready, hide all the error messages. Perform the validation task for all the input fields such as username, email, password, and confirm password. Example 1: This example ...
🌐
Kaylee42
kaylee42.github.io › blog › 2016 › 02 › 11 › intro-to-the-jquery-validation-plugin
Intro to the jQuery Validation Plugin - there be code here
It provides easy, immediate validation on forms, analyzing user input as they type and providing instant feedback on validation failures in addition to preventing a form from submitting if it contains any invalid data. However, I found the documentation a little lacking, especially for a JavaScript/jQuery beginner like myself. So I wanted to share a step-by-step implementation guide with a focus on customization. Here’s an example of it in action on Romp, a social networking application I built with @eaud and @japplaba in Rails along with jQuery and AJAX:
🌐
Jqueryvalidation
jqueryvalidation.org › documentation
Documentation | jQuery Validation Plugin
rule: A validation rule associates an element with a validation method, like "validate input with name "primary-mail" with methods "required" and "email". This library adds three jQuery plugin methods, the main entry point being the validate method:
Find elsewhere
🌐
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 - This tutorial shows how to set up a simple form validation example using a registration form. We’ll use the jQuery Validation Plugin to validate an HTML form.
🌐
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 - This is a short article on “how to add validations to an HTML form using jquery”. I will be using jQuery in this post to demonstrate how you can validate your form data before it is submitted. I will also write a separate post for doing form validations with vanilla JavaScript and link it at the end of this article when it's ready.
🌐
DevExtreme
js.devexpress.com › jQuery › Demos › WidgetsGallery › Demo › Validation › Overview
JavaScript/jQuery Validation - Overview | jQuery/JS Example
'password' : 'text'); }; $('#summary').dxValidationSummary({ }); $('#email-validation').dxTextBox({ inputAttr: { 'aria-label': 'Email' }, }) .dxValidator({ validationRules: [{ type: 'required', message: 'Email is required', }, { type: 'email', message: 'Email is invalid', }, { type: 'async', message: 'Email is already registered', validationCallback(params) { return sendRequest(params.value); }, }], }); $('#password-validation').dxTextBox({ mode: 'password', inputAttr: { 'aria-label': 'Password' }, onValueChanged() { const editor = $('#confirm-password-validation').dxTextBox('instance'); if (e
🌐
Jqueryvalidation
jqueryvalidation.org › jQuery.validator.addMethod
jQuery.validator.addMethod() | jQuery Validation Plugin
May 23, 2013 - Second argument: Validated element. Third argument: Parameters. ... parameters specified for the method, e.g. for min: 5, the parameter is 5, for range: [1, 5] it's [1, 5] ... The default message to display for this method. Can be a function created by ''jQuery.validator.format(value)''. When ...
🌐
Jqueryvalidation
jqueryvalidation.org › category › validator
Validator | jQuery Validation Plugin
Second argument: Validated element. Third argument: Parameters. ... parameters specified for the method, e.g. for min: 5, the parameter is 5, for range: [1, 5] it's [1, 5] ... The default message to display for this method. Can be a function created by ''jQuery.validator.format(value)''. When undefined, an existing message is used (handy for localization), otherwise the field-specific messages have to be defined.
🌐
GitHub
github.com › jquery-validation › jquery-validation
GitHub - jquery-validation/jquery-validation: jQuery Validation Plugin library sources · GitHub
$("#myForm").validate({ rules: { username: { required: true, // Using the normalizer to trim the value of the element // before validating it. // // The value of `this` inside the `normalizer` is the corresponding // DOMElement.
Starred by 10.3K users
Forked by 2.7K users
Languages   JavaScript 95.5% | HTML 4.5%
🌐
HTML Form Guide
html.form.guide › jquery › validation-using-jquery-examples
Form Validation Using Jquery Examples | HTML Form Guide
If you want to see how you can validate your form quickly usingthe jQuery Form validation plugging, head straight to The Most Indispensable jQuery Form Validation Reference Guide · To get started, include jQuery in your HTML file. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> Add the following form to the file. <!-- First Form --> <h2>Example 1:</h2> <form id="first_form" method="post" action=""> <div> <label for="first_name">First Name:</label> <input type="text" id="first_name" name="first_name"></input> </div> <div> <label for="last_name">Last Na
🌐
W3Schools
w3schools.com › js › js_validation.asp
W3Schools.com
jQuery Selectors jQuery HTML jQuery CSS jQuery DOM JS Graphics · JS Graphics JS Canvas JS Plotly JS Chart.js JS Google Chart JS D3.js JS Examples · JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... HTML form validation can be done by JavaScript.
🌐
Jquery
forum.jquery.com › portal › en › community › topic › jquery-validate-need-example-of-dynamic-validation
[jQuery] [validate] Need example of dynamic validation
Ask questions here if you're new to jQuery or JavaScript and need help making sense of it all · Ask questions and report issues related to using jQuery. Discuss anything related to jQuery itself. For issues with plugins, ask in the jQuery Plugins forum · Want to discuss the forum itself?
🌐
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 - Once we add the files, we can start using the validation plugin by invoking the .validate method. 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.
🌐
Syncfusion
support.syncfusion.com › kb › article › 6897 › how-to-use-our-js-components-with-jquery-validate-plugin
How to use our JS components with jquery validate plugin?
There are many inbuilt methods available within the JQuery.Validate plugin. The simplest way to validate your Form is to use the Validate() method like $("Form ID").validate();. It will validate your Form before submitting it.
🌐
Medium
medium.com › @merveyuksek › quick-form-validation-with-jquery-validation-f603e5d81bb0
Quick Form Validation With jQuery Validation | by Merve Yüksek | Medium
July 25, 2018 - $(document).ready(function() { $("form[name='registration']").validate({ rules: { firstname: "required", lastname: "required", email: { required: true, email: true }, password: { required: true, minlength: 6 } }, messages: { firstname: "Please enter your firstname", lastname: "Please enter your lastname", email: { required: "Please enter an email address", email: "Please enter a valid email address" }, password: { required: "Please provide a password", minlength: "Your password must be at least 6 characters long" }, }, submitHandler: function(form) { form.submit(); } }); }); ... That’s it, you’re done! Visit https://jqueryvalidation.org/ for more information.