jQuery Form Validation Before Submit Example

JQuery Validator plugin is a JavaScript library that helps to validate forms on web pages, Through this tutorial, we will show you how to validate forms on the client side using the jQuery validator plugin.

How to Validate Form Before Submit using jQuery?

Steps to validate forms on the client side before sending the form data to the server:

Step 1: Create HTML FORM

Create an HTML file named index.html and create a form with some fields like first name, last name, email, password in the index.html file.

<!DOCTYPE html>
<html>
<head>
<style>
label,
input,
button {
border: 0;
margin-bottom: 3px;
display: block;
width: 100%;
}
.common_box_body {
padding: 15px;
border: 12px solid #28BAA2;
border-color: #28BAA2;
border-radius: 15px;
margin-top: 10px;
background: #d4edda;
}
</style>
</head>
<body>
<div class="common_box_body test">
<h2>Registration</h2>
<form action="#" name="registration" id="registration">
<label for="firstname">First Name</label>
<input type="text" name="firstname" id="firstname" placeholder="John"><br>
<label for="lastname">Last Name</label>
<input type="text" name="lastname" id="lastname" placeholder="Doe"><br>
<label for="email">Email</label>
<input type="email" name="email" id="email" placeholder="[email protected]"><br>
<label for="password">Password</label>
<input type="password" name="password" id="password" placeholder=""><br>
<input name="submit" type="submit" id="submit" class="submit" value="Submit">
</form>
</div>
</body>
</html>

Step 2: Add the jQuery Validation Plugin

To add jQuery validation plugin cdn file in index.html file:

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.js"></script>

Step 3: Write jQuery Validation Rule

Writing validation rules with the help of jQuery validate() function for form fields to validate form data before submitting:

<script>
$(document).ready(function(){
$("#registration").validate({
// Specify validation rules
rules: {
firstname: "required",
lastname: "required",
email: {
required: true,
email: true
},
password: {
required: true,
minlength: 5
}
},

});
});
</script>

Test This Project

jQuery Form validator()

Registration





Recommended jQuery Tuorials

  1. jQuery Form Validation Custom Error Message
  2. jQuery AJAX Form Submit PHP MySQL
  3. Simple Registration Form in PHP with Validation
  4. jQuery Ajax Form Submit with FormData Example
  5. Codeigniter php jQuery Ajax Form Submit with Validation

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *