Validating form with jquery
Add these two script at top of your HTML file.
JQuery CDN link
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
JQuery Validate CDN link
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
name
attribute. Like <input type="email" name="email>
<input type="submit" value="Submit Form">
$(function() {
$("#loginForm").validate({
rules: {
email: {
required: true,
email: true,
},
password: {
required: true,
minlength: 5,
}
},
messages: {
email: {
required: "Please enter a valid email address.",
},
password: {
required: "Please provide a password.",
minlength: "Password is too short. Minimum length is 5."
}
},
submitHandler: function(form) {
form.submit();
}
})
})
${"#loginForm"}
. Replace this with your form id.messages
here you have to define your custom error message. The message which will appear when validation will fail.submitHandler
whose value is a function which detects when the form is submitted and shows error message if any.error
class.error
.
form .error {
color: red;
}