Solved

Javascript submit on enter error

Posted on 2010-09-23
7
311 Views
Last Modified: 2012-06-27
Ok...  Hopefully a simple fix...  I use this script to allow users to submit their search forms when they hit the enter key in the zip code field...  The problem I'm having is that if they have autocomplete on, and they type 43 and then the autocomplete shows 43123 under the field and the user clicks it and hits enter...  When it submits the search, it only registers the "43".  The autocomplete works fine without my script, but then it only submits if they hit submit...

Thanks...
onKeyPress="return submitenter(this,event)"


<SCRIPT TYPE="text/javascript">
<!--
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
        formMyForm = document.getElementById("home_search_form");
        formMyForm.action = "vehicle_search.php";
        formMyForm.submit();
   }
else
   return true;
}
//-->
</SCRIPT>

Open in new window

0
Comment
Question by:stephenmp
  • 4
  • 2
7 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 33750155
add this on top, it maybe what you are looking for

if (myfield.length < 5) return false;

user will need to enter again to submit
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33750312
Try onkeyup instead onkeypress
0
 

Author Comment

by:stephenmp
ID: 33750321
That's more a validator... I can do that with jquery...  I'm looking to move the onkeypress to something that will only call on enter when the autocomplete enter has been pushed...
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 82

Expert Comment

by:leakim971
ID: 33750327
Else try a timer :


<SCRIPT TYPE="text/javascript">
<!--
	function submitenter(myfield,e) {
		var keycode = (window.event)?(window.event.keyCode):(e.which);
		if(keycode == 13) setTimeout("afterHalf()", 500);
		return true;
	}
	function afterHalf() {
		formMyForm = document.getElementById("home_search_form");
		formMyForm.action = "vehicle_search.php";
		formMyForm.submit();
	}
//-->
</SCRIPT>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 33750519
Thanks for the points!
0
 

Author Comment

by:stephenmp
ID: 33750535
Thank you...
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33750536
You're very welcome!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question