0

I call this JS function on a submit to compare passwords. The console is showing me that the $ is not defined. Not sure what is going on?

 <input type="submit" name="submitButton" id="submitButton"  
  OnClick="CodeBehindMethod" runat="server" />


<script type="text/JavaScript">


$(document).ready(function () {
  $("#submitButton").click(
     function (e) {
         var pass = $('#password').val();
     if (!pass || pass === "") {
            this.errors.push("Please enter a password");
        }
        else if (pass !== $('#password1').val()) {
            this.errors.push("Passwords do not match");
        }
        this.printErrors();
    });
  });
John
  • 371
  • 2
  • 4
  • 16

2 Answers2

0

You should add library jquery inside your code.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Fakt309
  • 821
  • 5
  • 14
0

It sounds like you haven't added a reference to the jQuery library in your code. After you add it, it will work.

Example:

console.log($);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Your full code:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/JavaScript">


$(document).ready(function () {
  $("#submitButton").click(
   function (e) {
       var pass = $('#password').val();
   if (!pass || pass === "") {
          this.errors.push("Please enter a password");
      }
      else if (pass !== $('#password1').val()) {
          this.errors.push("Passwords do not match");
      }
      this.printErrors();
  });
});
  
</script>
mwilson
  • 12,295
  • 7
  • 55
  • 95
  • I added it and now it doesn't do anything. Is the JS method being called with the submitbutton definition I added to the question? – John Apr 02 '21 at 21:21