submit button doesn't work when keypress

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
valen53
Forum Contributor
Posts: 137
Joined: Tue Aug 27, 2002 9:29 am

submit button doesn't work when keypress

Post by valen53 »

i face a stranger problem.

Code: Select all

<script language="JavaScript1.2">
function mainvalidate(form)
&#123;
  if (document.forms&#1111;0].key.value=="") &#123;
     alert("Electronic key can not be blank ! ");
	return(false); 
  &#125;	
&#125;
</script>

<input name="Confirm" type="submit" id="Confirm" value="Confirm" onClick="return mainvalidate(this.form)">
submit button doesn't work when press enter key. But it work when use mouse clicking.

anyone can give me comment about that problem ?
thank u very much.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

In some browsers, pressing enter is not considered the same thing as physically selecting the submit button and clicking it. I know very little about JS but could you use the onSubmit event to run the validation instead of the onClick event?

Mac
User avatar
richie256
Forum Commoner
Posts: 37
Joined: Mon Oct 13, 2003 8:00 pm
Location: Montréal/Canada

Post by richie256 »

onSubmit is an attribute for <FORM ...>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Exactly, relying on a press of a submit button whether in JS or PHP is essentially flawed thanks to differing browser implementations.

Mac
User avatar
cybaf
Forum Commoner
Posts: 89
Joined: Tue Oct 01, 2002 5:28 am
Location: Gothenburg Sweden

Post by cybaf »

you could also set the action of the form to run the mainvalidate() function and thereafter change the action to where you want it to go and submit the form.
valen53
Forum Contributor
Posts: 137
Joined: Tue Aug 27, 2002 9:29 am

Post by valen53 »

thanks 4 all.

onSubmit was working. But when press the enter, the form cannot post, i dunno which part of my coding contain error.

Code: Select all

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
if ( $_POST&#1111;'Confirm'] == 'Confirm' ) &#123; 
    echo'
<script language="JavaScript">
     alert("  '.$Confirm.' Was Incorrect, Please Try Again ! ");
</script>
   ';  &#125;
?>
<form action="<? echo $PHP_SELF ?>" method="post" name="form1" onsubmit ="return mainvalidate(this.form)">
<input name="key" type="password" >
<input name="Confirm" type="submit" id="Confirm" value="Confirm" >
</body>
</html>
when mouse click, it will alert but enter key cannot.
i want to ask that mostly which part of code will cause that kind of error.
User avatar
cybaf
Forum Commoner
Posts: 89
Joined: Tue Oct 01, 2002 5:28 am
Location: Gothenburg Sweden

Post by cybaf »

try this:

Code: Select all

<html> 
<head> 
<title>Untitled Document</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript1.2"> 
function mainvalidate(form) 
&#123; 
  if (document.forms&#1111;0].key.value=="") &#123; 
     alert("Electronic key can not be blank ! "); 
   return(false); 
  &#125;    
&#125; 
</script>
</head> 
<body> 
<?php 
if ($_POST&#1111;'test']) &#123; 
    echo' 
<script language="JavaScript"> 
     alert("  '.$_POST&#1111;"Confirm"].' Was Incorrect, Please Try Again ! "); 
</script> 
   ';  &#125; 
?> 
<form action="<? echo $PHP_SELF ?>" method="post" name="form1" onSubmit ="mainvalidate(this.form)">
<input type="hidden" name="test" value="1">
<input name="key" type="password" > 
<input name="Confirm" type="submit" id="Confirm" value="Confirm" > 
</body> 
</html>
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

before you go changing your entire page, can you show us the html? and tell us where you were placing onclick versus onsubmit?

(i have a hunch i know what's causing that but need more to know if i'm right... which if i am there is likely nothing wrong per se, so the changes will be very minor, and i will explain it all if i am right, but to know that i need to see the html)
valen53
Forum Contributor
Posts: 137
Joined: Tue Aug 27, 2002 9:29 am

Post by valen53 »

thank for reply

i have try to test " cybaf " code already. But the problem still there.

when i use mouse clicking. it will popup
"Confirm Was Incorrect, Please Try Again ! "

but use keyboard enter,only popup
"Was Incorrect, Please Try Again ! "

The javascript working correctly.

i suprise that what's this problem .. b'cos my other file was work correctly. Then i use my other file to testing. Finally i found a quite funny solution.

i insert another textfield. Then the form just can post if press enter key.
Suprise, must be more than 1 textfield, form just can post if press enter key.
:lol: :lol:
User avatar
cybaf
Forum Commoner
Posts: 89
Joined: Tue Oct 01, 2002 5:28 am
Location: Gothenburg Sweden

Post by cybaf »

valen53 wrote:i have try to test " cybaf " code already. But the problem still there.
my bad... sorry about that... it seems as the submitbutton is not posted with the rest of the variables when the enterkey is used to submit the form. so if you test for some other variable istead of the submitbutton, you should be ok.
Post Reply