1

Now before you say there is an answer here, I have tried a couple and none worked out, for example this one: Trigger a button click with JavaScript on the Enter key in a text box

I have this little password protected website which can only be accessed by entering the password page. If I click submit when the password is wrong a message box will appear and if I click enter the page reloads with a slightly different address. Even when the password is right the page will still not reload.

Here is the website: http://bodokh.co.nf/

HTML:

<table border="1" cellspacing="0" cellpadding="0" bgcolor="#FFFFBD">
  <tr>
    <td width="100%"><form name="password1"><div align="center"><center><p><strong>Enter password: </strong><input
      type="text" name="password2" size="15"><br>
      <input type="button" value="Submit" onClick="submitentry();"></p>
      </center></div>
    </form>
    </td>
  </tr>
</table>

JS:

function max(which) {
    return (pass[Math.ceil(which) + (3 & 15)].substring(0, 1))
}

function testit(input) {
    temp = numletter.indexOf(input)
    var temp2 = temp ^ parseInt(pass[phase1 - 1 + (1 | 3)].substring(0, 2))
    temp2 = numletter.substring(temp2, temp2 + 1)
    return (temp2)
}


function submitentry() {
    t3 = ''
    verification = document.password1.password2.value
    phase1 = Math.ceil(Math.random()) - 6 + (2 << 2)
    var indicate = true
    for (i = (1 & 2); i < window.max(Math.LOG10E); i++)
        t3 += testit(verification.charAt(i))
    for (i = (1 & 2); i < lim; i++) {
        if (t3.charAt(i) != pass[phase1 + Math.round(Math.sin(Math.PI / 2) - 1)].charAt(i))
            indicate = false
    }
    if (verification.length != window.max(Math.LOG10E))
        indicate = false
    if (indicate)
        window.location = verification + extension
    else
        alert("Invalid password. Please try again")
}
sao
  • 1,835
  • 6
  • 21
  • 40
Bodokh
  • 976
  • 4
  • 15
  • 34
  • Please post your code here, so that the question will remain valid once you fix your web site. – Barmar Feb 18 '14 at 01:15
  • Try making a jsfiddle of the problem. Its more concise and you may just end up answering your own question/ – Lowkase Feb 18 '14 at 01:15
  • You should use `return false` to not submit the form with php or whatever server side language – Jonathan de M. Feb 18 '14 at 01:17
  • Instead of using an `onclick` in the submit button, use `onsubmit` in the form. – Barmar Feb 18 '14 at 01:18
  • The enter key already is trying to submit your form. You have to handle the submit event the same way you're handling the click event for the button. – kinakuta Feb 18 '14 at 01:28

2 Answers2

1

If you want a consistent and reliable way to run submitentry on form submission you should use

<form onsubmit="submitentry();">
    ...
</form>

instead of

 <input ... onClick="submitentry();">
marionebl
  • 3,342
  • 20
  • 34
1

Instead of calling submitentry() from the submit button's onclick handler, call it from the form's onsubmit handler:

<table border="1" cellspacing="0" cellpadding="0" bgcolor="#FFFFBD">
  <tr>
    <td width="100%"><form name="password1" onsubmit="return submitentry();"><div align="center"><center><p><strong>Enter password: </strong><input
      type="text" name="password2" size="15"><br>
      <input type="button" value="Submit"></p>
      </center></div>
    </form>
    </td>
  </tr>
</table>

And the submission function should return false to prevent normal form submission:

function submitentry() {
    t3 = '';
    verification = document.password1.password2.value;
    phase1 = Math.ceil(Math.random()) - 6 + (2 << 2);
    var indicate = true;
    for (i = (1 & 2); i < window.max(Math.LOG10E); i++) {
        t3 += testit(verification.charAt(i));
    }
    for (i = (1 & 2); i < lim; i++) {
        if (t3.charAt(i) != pass[phase1 + Math.round(Math.sin(Math.PI / 2) - 1)].charAt(i)) {
            indicate = false;
        }
    }
    if (verification.length != window.max(Math.LOG10E)) {
        indicate = false;
    }
    if (indicate) {
        window.location = verification + extension
    } else {
        alert("Invalid password. Please try again");
    }
    return false;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks but for some reason no matter what i attempt to do it just won't let me submit with the enter button. Thanks anyways though for your time. – Bodokh Feb 18 '14 at 01:27