Error in Ajax Function

This is ajax function which i need to use, While execute the code it is showing that “)” is unexpected token

function ChangePassword() {
  var email = $("#txtForgotPassEmail").val();
  if (email == "") {
    alert("Please enter the Email ID.")
  } else if (!ValidateEmail(email)) {
    alert("please enter valid Email ID.")
  } else {
    $.ajax({
      type: "POST",
      dataType: "json",
      data: {
        "email": email
      },
      url: "/MainIndex/forgotPassword",
      success: function(val) {
        if (val == "0") {
          alert("Please enter the Valid Email ID.")
          $("#txtForgotPassEmail").val("");
        } else {
          $("#ForgotPassDiv").hide();
          $("#ChangePassDiv").show();
        }
      }
    });
  }
});
}

Error-Uncaught SyntaxError: Unexpected token )

there is syntax error on above ajax function



You add two “});”… see below
your code

function ChangePassword() {
  var email = $("#txtForgotPassEmail").val();
  if (email == "") {
    alert("Please enter the Email ID.")
  } else if (!ValidateEmail(email)) {
    alert("please enter valid Email ID.")
  } else {
    $.ajax({
      type: "POST",
      dataType: "json",
      data: {
        "email": email
      },
      url: "/MainIndex/forgotPassword",
      success: function(val) {
        if (val == "0") {
          alert("Please enter the Valid Email ID.")
          $("#txtForgotPassEmail").val("");
        } else {
          $("#ForgotPassDiv").hide();
          $("#ChangePassDiv").show();
        }
      }
    });
  }
});
}

After change

function ChangePassword() {
  var email = $("#txtForgotPassEmail").val();
  if (email == "") {
    alert("Please enter the Email ID.")
  } else if (!ValidateEmail(email)) {
    alert("please enter valid Email ID.")
  } else {
    $.ajax({
      type: "POST",
      dataType: "json",
      data: {
        "email": email
      },
      url: "/MainIndex/forgotPassword",
      success: function(val) {
        if (val == "0") {
          alert("Please enter the Valid Email ID.")
          $("#txtForgotPassEmail").val("");
        } else {
          $("#ForgotPassDiv").hide();
          $("#ChangePassDiv").show();
        }
      }
    });
  }
/* you add extra }); */
}

FOR MORE JQUERY SOLUTION AND EXAMPLE VISIT

There is single mistake.. when you close the else portion then you used braces that’s why it shows syntax error