/    Sign up×
Community /Pin to ProfileBookmark

Submit form when return key is pressed?

On some browsers (IE) this form is cleared when return is pressed. I would like it to submit the form when return is pressed. It works on Safari, but I need it to work on IE too. Is there a way to do it? Here’s my form code (abridged):

<script type=”text/javascript” language=”javascript”>
<!–
function validate(zipinfo)
{
if (zipinfo.z.value == “”)
{
alert(“Please enter a 5-digit zip code.”);
zipinfo.z.focus();
return (false);
}
window.open(‘http://www.zipwise.com/info-tool.php?z=’+zipinfo.z.value+’&loc=’+location.href,”,’toolbar=0,location=0,directories=0,status=0,me nubar=0,scrollbars=0,resizable=yes,height=300,width=350′,true);
}
//–>
</script>

<form name=”zipinfo”>
<input name=”z” type=”text” id=”z”>
<input type=”submit” value=”Submit” onClick=”return validate(zipinfo);”>
</form>

Thanks for any suggestions.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KorAug 11.2004 — Try
[code=php]
<script type="text/javascript" language="javascript">
<!--
function validate(){
if (document.zipinfo.z.value == ""){
alert("Please enter a 5-digit zip code.");
document.zipinfo.z.focus();
return false;
}
else{
window.open('http://www.zipwise.com/info-tool.php?z='+zipinfo.z.value+'&loc='+location. href,'','toolbar=0,location=0,directories=0,status
=0,me nubar=0,scrollbars=0,resizable=yes,height=300,widt
h=350',);
return true;
}
}
//-->
</script>

<form name="zipinfo" onsubmit="return validate();>
<input name="z" type="text" id="z">
<input type="submit" value="Submit">
</form>
[/code]


The validate function should be called onsubmit event, not onclick, as the button, if it is a submit type, will do its job anyway, after the onclick event is handled.
Copy linkTweet thisAlerts:
@briandunningauthorAug 11.2004 — That did it - I don't know what I was thinking about. ?
Copy linkTweet thisAlerts:
@KorAug 11.2004 — you're welcome here! We all are glad to have had helped anyone as our knowledge had let us on ?
×

Success!

Help @briandunning spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...