HOME | FERGUSON Digital Blog

Prevent ENTER key from completing forms in CFDIV

ColdFusion , General Topics Add comments

When presenting the user with several form fields to complete inside a CFDIV, it may be necessary to prevent them from pressing the enter key on the keyboard within the form. Here is the scenario, the form is expecting at least two text fields to be completed, but the user presses enter after the first one. You will have to anticipate this in the form data collection, but you were more concerned at the time with filtering the submission for erroneous characters or malicious submissions.

For each text field in the form, a JavaScript call to a function that blocks the captured keycode (13 for the enter key) will solve this problem. Why worry about this in a CFDIV? If you submit your form in a CFDIV and the entries are incomplete, the first reaction the user will have may be to press the browser back button. Since a CFDIV with a form is actually Ajax, there is nothing cached for the browser to back history.

Inside our main page is our JavaScript function:

<script type="text/javascript">
   fnNoEnter=function() {
      return !(window.event && window.event.keyCode==13);}
</script>

The form inside the CFDIV could be represented as the following:

<cfform action="#CGI.SCRIPT_NAME#" method="get" name="FormEmployee">
   <input name="NameFirst" type="text" onkeydown="return fnNoEnter();" value="" />
   <input name="NameLast" type="text" onkeydown="return fnNoEnter();" value="" />
   <input name="Submit" type="submit" value="SUBMIT" />
</cfform>

This level of input checking is client-side only and can be defeated if the user browser does not have JavaScript enabled. In a corporate setting, where each client browser is uniformly configured through a group policy however, it can be very effective.

If you find this post useful please leave a comment and let me know how you used the information.

0 responses to “Prevent ENTER key from completing forms in CFDIV”

Leave a Reply




© Copyright 1997-2024, All Rights Reserved Coldfusion and MS SQL2008
Powered by Mango Blog.   Design by FERGUSON Digital
o:Event name="beforeHtmlBodyEnd" />