/    Sign up×
Community /Pin to ProfileBookmark

Submit when press enter

I have a problem.
When I click on button ok, it works

but how can I also make it work when I press the enter key??
is there a method like onEnter or something?

here is the code:

<html>
<HEAD>
<SCRIPT LANGUAGE=”JavaScript”>
function checkDecimals(fieldName, fieldValue) {
if (isNaN(fieldValue)) {
alert(“Oops! That does not appear to be a valid number. Please try again.”);
fieldName.select();
fieldName.focus();
}
if(fieldValue == “”) {
alert(“Oops! The input box is empty. Please try again.”);
fieldName.select();
fieldName.focus();
}
}
</script>
</HEAD>
<BODY>
<center>
<form action=”test.html” method=”post” >
Please enter a number with up to 2 decimal places: <br>
<input type=text name=”aantalbest”>
<input type=button name=ok value=”Ok” onClick=”return checkDecimals(this.form.aantalbest, this.form.aantalbest.value)” >
</form>
</center>
</body>
</html>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@FangApr 30.2003 — Here's the basic code:

<script language="javascript">

<!--

document.onkeypress = keyhandler;

function keyhandler(e) {

var event = e ? e : window.event;

if (event.keyCode == 13) { alert("Enter Pressed");}

}

// -->

</script>
Copy linkTweet thisAlerts:
@CharlesApr 30.2003 — [font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">

<html>

<HEAD>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<title>Some Title</title>

<SCRIPT LANGUAGE="JavaScript">

function checkDecimals(f) {

if (isNaN(f.aantalbest.value) || !/^d*(.d{0,2})?$/.test(f.aantalbest.value)) {

alert("Oops! That does not appear to be a valid number. Please try again.");

f.aantalbest.value = '';

f.aantalbest.focus();

return false;

}

if(f.aantalbest.value == '') {

alert("Oops! The input box is empty. Please try again.");

f.aantalbest.value = '';

f.aantalbest.focus();

return false;

}

}

</script>

</HEAD>

<BODY>

<center>

<form action="test.html" method="post" onsubmit="return checkDecimals(this)">

Please enter a number with up to 2 decimal places: <br>

<input type=text name="aantalbest">

<input type=submit name=ok value="Ok")" >

</form>

</center>

</body>

</html>[/font]

[font=georgia]And I've taken the liberty of adding some range checking. This form will reject any number with more than two decimal places.[/font]
Copy linkTweet thisAlerts:
@JonaApr 30.2003 — In your <form> tag: onsubmit="return checkDecimals(this.form.aantalbest, this.form.aantalbest.value)"

And make your "ok" button <input type=[b]submit[/b]>.

Also, in your code, add return false; twice, like this:

function checkDecimals(fieldName, fieldValue) {

if (isNaN(fieldValue)) {

alert("Oops! That does not appear to be a valid number. Please try again.");

fieldName.select();

fieldName.focus();

[b]return false;[/b]

}

if(fieldValue == "") {

alert("Oops! The input box is empty. Please try again.");

fieldName.select();

fieldName.focus();

[b]return false;[/b]

}

}
×

Success!

Help @rbollen 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.27,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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