0

I have a login page where the user enters the employee id .And upon entering I am trying validate and if certain condition is satisfied I will redirect to another page if not display an alert saying invalid input.

1st page code:

function alphanumOnly(input) {
  var regex = /[^a-zA-Z0-9]/gi;
  input.value = input.value.replace(regex, "");
}

function getRepoCount() {
  alert(1);
  var empid = document.getElementById("ManagerId").value;
  alert(empid);
  $.ajax({
    url: 'http://localhost:8088/JirasTrackingApp/reporter/Reportees/ReporteeList/'+ empid,
    type: 'GET',
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    success: function(result) {
      var size = result.length;
      if (size > 0) {
        window.location.href = "indextable.html";
        return true;
      } else {
        alert("Please enter an valid input");
        return false;
      }
    },
    /*error:function(result){
        return false;
        alert("Please enter an valid input");
    },*/

  });
}
<form action="indextable.html" method="GET">
  <p><input name="ManagerId" class="form-control" id="ManagerId" type="text" autocomplete="off" onkeyup="alphanumOnly(this)" maxlength="8" placeholder="ManagerId"></input>
  </p>
  <p> <input type="button" onclick="return getRepoCount();" value="submit">Submit</body>
  </p>
</form>

My 2nd html page:

<!DOCTYPE html>
<html>
  <head>
    <script>
      const urlParams = new URLSearchParams(window.location.search);
      const myParam = urlParams.get('ManagerId');
      alert(myParam); //This displays null
      getReporteeList(myParam);
    </script>

My index.js file which actually contains the getReportees()

If I use the input type button and onclick function that will make an ajax call and validate. But if I give an valid input it should get redirected to 2nd html page, but after redirecting it displays the null in alert. So the input value is not being read in 2nd html page. But if I give invalid input it shows the alert message as invalid input in the first login page only.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Ramya
  • 49
  • 6
  • I made you a snippet so I could read your code. You need to use the submit event and preventDefault - your return false does not work and you do not currently pass the ManagerId in the location change in the success – mplungjan Feb 02 '19 at 09:47
  • So : `$("form").on("submit",function(e) { e.preventDefault(); getRepoCount() })` using `success: function(result) { var size = result.length; if (size > 0) { window.location.href = "indextable.html?ManagerId="+encodeURIComponent(empid);` – mplungjan Feb 02 '19 at 09:48

0 Answers0