Form Validation in JQuery with Example

Amit Kumar
2 min readJul 30, 2021

In this tutorial i’m going to implement form validation using Jquery in PHP. Mentioned below.

What is FormValidation?

Form validation is a process of confirming the relevant information entered by the user in the input field. In this tutorial im using simple form that consists of a name, email, contact, password and a confirm password using jQuery.

Go to file and create index.php file and paste below code.

<!DOCTYPE html>
<html>

<head>
<title>Form Validation</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" />
</head>

<body class="container">
<div class="row mt-3" style="margin-left: 204px">
<div class="col-md-8">
<h4 class="mb-3">Form Validation in PHP - <a href="https://thedataops.org/" target="_blank" rel="noopener noreferrer">LaravelAmit</a>
</h4>
<form id="form" method="post" action="getData.php">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="name">
</div>
<div class="form-group">
<label for="email">Email Address</label>
<input type="text" class="form-control" name="email" id="email">
</div>
<div class="form-group">
<label for="contact">Contact</label>
<input type="text" class="form-control" name="contact" id="contact">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" id="password">
</div>
<div class="form-group">
<label for="confirmPassword">Confirm Password</label>
<input type="password" class="form-control" name="confirmPassword" id="confirmPassword">
</div>
<input type="submit" class="btn btn-primary" value="Submit" />
</form>
</div>
</div>
</body>
<style>
.error {
color: red;
}
</style>
<script>
$(document).ready(function () {
$('#form').validate({
rules: {
name: {
required: true
},
email: {
required: true,
email: true
},
contact: {
required: true,
rangelength: [10, 12],
number: true
},
password: {
required: true,
minlength: 8
},
confirmPassword: {
required: true,
equalTo: "#password"
}
},
messages: {
name: 'Please enter Name.',
email: {
required: 'Please enter Email Address.',
email: 'Please enter a valid Email Address.',
},
contact: {
required: 'Please enter Contact.',
rangelength: 'Contact should be 10 digit number.'
},
password: {
required: 'Please enter Password.',
minlength: 'Password must be at least 8 characters long.',
},
confirmPassword: {
required: 'Please enter Confirm Password.',
equalTo: 'Confirm Password do not match with Password.',
}
},
submitHandler: function (form) {
form.submit();
}
});
});
</script>

</html>

Now form validation look like this

Thanks 👍👍

--

--

Amit Kumar

Hi I am Amit Experienced Web Developer with a demonstrated history of working in the information technology and services industry.