Home > Software engineering >  Trying To Match Two Fields Before Form Submit With Javascript
Trying To Match Two Fields Before Form Submit With Javascript

Time:04-22

I am trying to match two fields with the result in my javascript before the form gets submitted but not working.

I don't want the form to get submitted if the results don't match.

Please what is the solution? Thanks.

Code Below;

function Check(){
    var done=0;
    var check=document.check.phone.value;
    var check=document.check.pcode.value;
    
    if (phone=="08023" && pcode=="12345") {
 alert ("Result Match!");
              
                done=1;
return fasle;

    }
    if (done==0) {
   
alert("Result Don't Match!");

}
}
<form action=" " method="post">
 
<input name="phone" maxlength="11"  type='tel' id="phone" placeholder="0000-000-0000" required="" />
          <br/><br/>
             
<input name="pcode" maxlength="11"  type='tel' id="pcode" placeholder="0000-000-0000" required="" />

<br/><br/>

 <button  onClick="javascript:Check()"  name="Submit" type="submit" value="Login"><span>Log In </span></button> 

</form>

CodePudding user response:

You have several mistakes in your code, I have cleaned it up a bit for you.

        function Check() {
            var done = 0;
            var phone = document.check.phone.value;
            var pcode = document.check.pcode.value;

            if (phone=="08023" && pcode=="12345") {
                alert("Result Match!");
                done = 1;
            } else {
                alert("Result Don't Match!");
            }
        }
    <form name="check" action=" " method="post">
        <input name="phone" maxlength="11" type='tel' id="phone" placeholder="0000-000-0000" required="" />
        <br /><br />
        <input name="pcode" maxlength="11" type='tel' id="pcode" placeholder="0000-000-0000" required="" />
        <br /><br />
        <button  onClick="javascript:Check()" name="Submit" type="submit" value="Login">
            <span>LogIn</span></button>
    </form>

  • Related