-1

I am able to validate my form using javascript, but even if the form data is invalid the next process is done with regards to the submit button. I am unable to stop the page from loading the other page that is supported to be loaded only if there are no errors in my form after javascript validation

script type="text/javascript">
function matchpass(){ 

    var firstpassword=document.myform.psw.value;
    var secondpassword=document.myform.pswrepeat.value;  
    var x=document.myform.email.value;  
    var atposition=x.indexOf("@");  
    var dotposition=x.lastIndexOf(".");  

    if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length)
    {  
          alert("Please enter a valid e-mail address \n atpostion:"+atposition+"\n dotposition:"+dotposition);  
          return false;  
    } 

        if(firstpassword==secondpassword)
        {  
            return true;  
        }  
        else
        {  
            alert("password must be same!");  
        return false;  
        }  

    }

</script>
</head>
<body>
</div>

    <form name="myform"  action="RegisterControllerServlet" method="post"  >

    <input type="hidden" name="command" value="ADD">

  <div class="container">

    <p>Please fill in this form to create an account.</p>
    <hr>

     <label for="email"><b>Enter your Employee id</b></label>
    <input type="text"  name="de_eid"  >


    <label for="email"><b>Email</b></label>
    <input type="text"  name="email">

      <Select name="is" class="custom-select" style="width:500px; background-color: #A0A0A0; height:40px;">
    <option value="">Choose Occupation</option>
    <option value="Doctor">Doctor</option>
  <option value="Receptionist">Receptionist</option>
  <option value="labassistant">Lab assistant</option>
  <option value="pharmacist">Pharmacist</option>
    </Select>

    <br><br>
    <label for="psw"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="psw" required>

    <label for="psw-repeat"><b>Repeat Password</b></label>
    <input type="password" placeholder="Repeat Password" name="pswrepeat" required>
    <hr>


    <button type="submit" class="registerbtn" onclick="matchpass();">Register</button>
  </div>

I want the page not to redirect the user to the page that is supposed to be loaded only if there are no errors in validation

George Silva
  • 385
  • 5
  • 13
  • I don't see anything in the code that should cause a redirect. Do you have another function somewhere with the redirect code? – tdammon May 07 '19 at 17:05
  • 1
    Possible duplicate of [JavaScript code to stop form submission](https://stackoverflow.com/questions/8664486/javascript-code-to-stop-form-submission) – Alon Eitan May 07 '19 at 17:08

1 Answers1

0

Your <form> tag should have the onsubmit property -- not the <button>'s onclick at the bottom.

<form
    name="myform"
    action="RegisterControllerServlet"
    method="post"
    onsubmit="return matchpass()"
>
arthurakay
  • 5,631
  • 8
  • 37
  • 62