Link to home
Start Free TrialLog in
Avatar of stephenmp
stephenmpFlag for United States of America

asked on

Javascript submit on enter error

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

Avatar of HainKurt
HainKurt
Flag of Canada image

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
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stephenmp

ASKER

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...
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

Thanks for the points!
Thank you...
You're very welcome!