944,012 Members | Top Members by Rank

Ad:
Oct 15th, 2009
0

Disable "Enter" in radio button area in web form

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
serendipity is offline Offline
58 posts
since May 2007
Oct 17th, 2009
0
Re: Disable "Enter" in radio button area in web form
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.
Reputation Points: 82
Solved Threads: 74
Posting Pro in Training
Graphix is offline Offline
401 posts
since Aug 2009
Oct 17th, 2009
0
Re: Disable "Enter" in radio button area in web form
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.
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,527 posts
since Apr 2009
Oct 18th, 2009
0
Re: Disable "Enter" in radio button area in web form
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!!!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
serendipity is offline Offline
58 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Own html editor
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Best Way to Learn?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC