Disable "Enter" Key in Form script


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2.  
  3. /***********************************************
  4. * Disable "Enter" key in Form script- By Nurul Fadilah([email protected])
  5. * This notice must stay intact for use
  6. * Visit http://www.dynamicdrive.com/ for full source code
  7. ***********************************************/
  8.  
  9. function handleEnter (field, event) {
  10. var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  11. if (keyCode == 13) {
  12. var i;
  13. for (i = 0; i < field.form.elements.length; i++)
  14. if (field == field.form.elements[i])
  15. break;
  16. i = (i + 1) % field.form.elements.length;
  17. field.form.elements[i].focus();
  18. return false;
  19. }
  20. else
  21. return true;
  22. }
  23.  
  24. </script>
  25.  
  26. <!-- HTML -->
  27. <form>
  28. <input type="text" onkeypress="return handleEnter(this, event)"><br>
  29. <input type="text" onkeypress="return handleEnter(this, event)"><br>
  30. <textarea>Some text</textarea>
  31. </form>

URL: http://www.dynamicdrive.com/dynamicindex16/disableenter.htm

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.