Disable "Enter" in radio button area in web form

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: May 2007
Posts: 56
Reputation: serendipity is an unknown quantity at this point 
Solved Threads: 0
serendipity's Avatar
serendipity serendipity is offline Offline
Junior Poster in Training

Disable "Enter" in radio button area in web form

 
0
  #1
Oct 15th, 2009
I have a web form. I want the "enter" key disabled from sending the form so users have to SUBMIT in order to send form.
Have the following entered in the head:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function stopRKey(evt) {
  2. var evt = (evt) ? evt : ((event) ? event : null);
  3. var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  4. if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
  5. }
  6.  
  7. document.onkeypress = stopRKey;

It works great in the "text" form area which is in a table format. Sample:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <TR>
  2. <TD align=right width=40%><FONT
  3. size=2><B>First Name:&nbsp;</b></FONT></TD>
  4. <TD ><INPUT name=A-firstname></TD></tr>
  5. <TR> <TD align=right width=40%><FONT
  6. size=2><B>Last Name:&nbsp;</b></FONT></TD>
  7. <TD><INPUT name=B-lastname></TD></TR>
When I get to the radio button area it doesn't work. It is NOT in a table format. Here's a sample of the radio button area:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <p> 1. The fundamental building blocks of matter are atoms and _________.<BR>
  2. <INPUT type=radio value="a" name="1"> a. mass <BR>
  3. <INPUT type=radio value="b" name="1"> b. protons <BR>
  4. <INPUT type=radio value="c" name="1"> c. molecules <BR>
  5. <INPUT type=radio value="d" name="1"> d. neutrons </p>
Any help would be greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 88
Reputation: Graphix is an unknown quantity at this point 
Solved Threads: 20
Graphix's Avatar
Graphix Graphix is offline Offline
Junior Poster in Training
 
0
  #2
Oct 17th, 2009
Perhaps try something like this?:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. ...
  2. ...Header HTML
  3. ...
  4. <script type="text/javascript">
  5. function entersubmit(event) {
  6. if (window.event && window.event.keyCode == 13) {
  7. return false;
  8. } else {
  9. if (event && event.which == 13) {
  10. return false;
  11. } else {
  12. return false;
  13. }
  14. }
  15. }
  16. </script>
  17. <form>
  18. <p> 1. The fundamental building blocks of matter are atoms and _________.<BR>
  19. <input type=radio value="a" name="1" onkeypress="entersubmit(event)"> a. mass <BR>
  20. <input type=radio value="b" name="1" onkeypress="entersubmit(event)"> b. protons <BR>
  21. <input type=radio value="c" name="1" onkeypress="entersubmit(event)"> c. molecules <BR>
  22. <input type=radio value="d" name="1" onkeypress="entersubmit(event)"> d. neutrons </p>
  23. </form>
  24. ...
  25. ...Footer HTML
  26. ...

EDIT 1: Also keep in mind that the usage of caps is depercated in XHTML.

EDIT 2: I also don't get the question. Atoms are build out of neutrons, elektrons and protons. Molecules are build out of atoms. And mass is just a wrong answer. Isn't the question wrong?

~G
Last edited by Graphix; Oct 17th, 2009 at 12:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 885
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 127
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark
 
0
  #3
Oct 17th, 2009
Serendipity,

The easiest way, in my experience, is not to use a submit button.

Instead, use a regular <input type="button" value="Submit" onclick"this.form.submit();"> . The script gives the necessary submit action.

Thus, the only way to submit the form should be for the user to click the "Submit" button (or to hit enter if/when it has focus).

I used to be sure that this technique was universally true, but you should test the latest crop of browsers to make sure. I could be out of date.

The slight downside is that <form ...... onsubmit="....."> (should you need it) will now not intercept the submission, but the workaround is also simple; modify the "Submit" button's onclick"this.form.submit();" to make submission conditional or whatever you would otherwise put in the form's onsubmit . If it gets complex, then write/call a function.

Here is a demo which you can use to do some testing:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <head>
  5. <title>Airshow :: Untitled</title>
  6. <style type="text/css">
  7. form { margin-top:20px; padding:12px; width:150px ; border:1px solid #999; }
  8. </style>
  9.  
  10. <script>
  11. function writeInputText(){
  12. document.f1.i1.value = 'ABCDEF';
  13. document.f2.i1.value = 'ABCDEF';
  14. }
  15.  
  16. onload = function(){
  17. setTimeout("writeInputText()", 1000);
  18. }
  19. </script>
  20. </head>
  21.  
  22. <body>
  23.  
  24. <form name="f1" action="">
  25. <input name="i1" value="------"><br>
  26. <input><br>
  27. <input type="radio" name="x" value="1"><br>
  28. <input type="radio" name="x" value="2"><br>
  29. <input type="radio" name="x" value="3"><br>
  30. <input type="button" value="My Submit" onclick="this.form.submit();"><br>
  31. </form>
  32.  
  33. <form name="f2" action="">
  34. <input name="i1" value="------"><br>
  35. <input><br>
  36. <input type="radio" name="x" value="1"><br>
  37. <input type="radio" name="x" value="2"><br>
  38. <input type="radio" name="x" value="3"><br>
  39. <input type="Submit" value="HTML Submit"><br>
  40. </form>
  41.  
  42. </body>
  43. </html>

Airshow
Last edited by Airshow; Oct 17th, 2009 at 1:14 pm.
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 56
Reputation: serendipity is an unknown quantity at this point 
Solved Threads: 0
serendipity's Avatar
serendipity serendipity is offline Offline
Junior Poster in Training
 
0
  #4
Oct 18th, 2009
Thanks Graphix & Airshow!Graphix, the "fundamental building blocks of matter" are atoms and molecules. You are right, atoms are made of protons, neutrons, electrons, and matter has mass, but is not made up of mass. Matter is also not made up of just protons, neutrons, or electrons. It is made up of atoms and molecules, and yes, molecules are made up of atoms! Hey, that's the point of multiple choice questions, isn't it? -- tricky tricky tricky!!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 702 | Replies: 3
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC