Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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?

irt.org | Knowledge Base | JavaScript | Form 7.1 | Q291 [ previous next ]

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?

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?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.