Why is jquery.validation.js not working in my project? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Why is jquery.validation.js not working in my project? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am currently learning jQuery and using jquery.validate.js to validate a form field on the client side. The thing is that I can’t get any output.

I have looked at the console; I target the an input field and dumped its attribute to console and it worked, when I use jquery.validate.js though, nothing.

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Validate</title>
  <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
</head>
<body>
  <div class="container">
    <form id="register">
      <h2 class="display-4 mb-4">Registration Form</h2>
      <div class="form-group">
        <label for="email">Email</label>
        <input name="email" placeholder="Email address" type="text" class="form-control">
      </div>
      <input class="btn btn-primary mt-5" id="submit-button" type="submit" value="Sign Up" role="button">
    </form>
  </div>
  <script src="jquery-3.5.1.min.js"></script>
  <script src="jquery.validate.js"></script>
  <script src="validation.js"></script>
$(document).ready(function() {
  $("#register").validate({
    debug: true,
    rules: {
      required: true,
      email: true
    },
    messages: {
      required: 'You have to enter a valid email address',
      email: 'You rmail is not valid'
    }
  });
});

Any help concerning the subject is appreciated.

Answer

The syntax for your validate() method settings isn’t quite right. There is no name="required" element in the DOM. That property should be under email, like this:

$("#register").validate({
  debug: true, // remember to remove this after testing :)
  rules: {
    email: { 
      required: true
    }
  },
  messages: {
    email: {
      required: 'You have to enter a valid email address'
    }
  }
});
We are here to answer your question about Why is jquery.validation.js not working in my project? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji