• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Jscript to stop enter submitting form

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    Jscript to stop enter submitting form

    Trying to write some online invoicing software and want to prevent submitting partially filled form with enter. Used the solutions on the net, ie
    stuck onkeydown='noenter();' on inputs and added some jscript:

    function noenter()
    {
    var x = event.which || event.keyCode;
    if (x == 13) return false;
    }

    Put same event code in the jscript on submit form validation and eventually got it to almost work by sticking onkeydown on ALL controls, even disabled ones, but if a control has been selected (even a disabled one!) and then enter is pressed it submits.

    Sounds trivial I know, but this is for a severely untechy person and it needs to be foolproof as possible. Any ideas why having a selected control causes the jscript to be ignored? Cheers.
    Last edited by xoggoth; 31 May 2016, 14:31.
    bloggoth

    If everything isn't black and white, I say, 'Why the hell not?'
    John Wayne (My guru, not to be confused with my beloved prophet Jeremy Clarkson)

    #2
    Try something like:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    
    function disableEnter(e) {
      if(e.which==13 || e.keyCode==13) 
      {
        e.preventDefault();
      } 
    }
    
    </script>
    </head>
    <body>
    <form method="post" onkeypress="disableEnter(window.event)">
    <input type=text/>
    <input type="submit"/>
    </form>
    </body>
    </html>

    Comment


      #3
      Ignore the above - doesn't work in IE10

      Comment


        #4
        Code:
        function checkIfTheFormIsReadyToBeSubmittedYetOrIfTheyMissedABit() {
            // check stuff here...
            if (everythingIsOk) {
                return true;
            }
            return false;
        }
        
        document.getElementById("theForm").onsubmit = checkIfTheFormIsReadyToBeSubmittedYetOrIfTheyMissedABit;
        // N.B. No brackets at the end of the line above is intentional - we want to assign the function, not the result of calling it :-)
        should do it - also stops them submitting it via the submit button, which you presumably also don't want if the form isn't complete

        Comment

        Working...
        X