Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q291 In a form one with only one text field how can you ensure that the form is only submitted if the submit button has been pressed?

You are here: irt.org | FAQ | JavaScript | Form | 7.1 | Q291 [ previous next ]

Its the opposite question to "How can I submit a form which has more than one text field by just pressing enter?" You can either add a another text field, or, you can trap the user pressing the enter key, rather than clicking the submit button by using a boolean flag:

<SCRIPT LANGUAGE="JavaScript"><!--
var clickedButton = false;
//--></SCRIPT>

<FORM onSubmit="return clickedButton">
<INPUT TYPE="TEXT">
<INPUT TYPE="SUBMIT" onClick="clickedButton=true">
</FORM>

You might want to reset the boolean falg after the form has been submitted, especially if submitting the form the client side processing:

<SCRIPT LANGUAGE="JavaScript"><!--
var clickedButton = false;

function check() {
    if (clickedButton) {
        clickedButton = false;
        return true;
    }
    else
        return false;
}
//--></SCRIPT>

<FORM onSubmit="return check()">
<INPUT TYPE="TEXT">
<INPUT TYPE="SUBMIT" onClick="clickedButton=true">
</FORM>

Feedback on 'Q291 In a form one with only one text field how can you ensure that the form is only submitted if the submit button has been pressed?'

©2018 Martin Webb