Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    New Coder
    Join Date
    Mar 2003
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Stopping <Enter> from submitting

    I have a form in which I have overriden the onSubmit to be 'return false'. I did this in hopes of stopping submission 100% of the time. Then, the specific button/link I want to submit handles the onClick event handler and all is well. This works great except that <Enter> still submits - as if the onClick gets handled since the button is a default 'submit' type. It only does this when in an edit field - when I am, press enter and the form submits. HELP?
    Brad Eck
    www.sitesdynamic.com

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Move whatever you have in the onclick of the submit button to the onsubmit of the forms, and it should work just fine.

    Remember that people with javascript turned off will still submit despite the onsubmit, though.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New Coder
    Join Date
    Mar 2003
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Then I would get the submission no matter what - I am trying to eliminate it. All the code works otehrwise.

    It turns out if I do not have a 'submit' type input field the onClick will not be called for submission. I just change the type to 'button' and everything is working as desired.

    Thanks!
    Brad Eck
    www.sitesdynamic.com

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, if you have

    <form onsubmit="return false">

    Nothing will submit it, even enter, unless the user has javascript disabled - in which case you'd need to use server-side checking for redunancy anyway.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #5
    New Coder
    Join Date
    Mar 2003
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's what I thought - until it acted otherwise. Even if I have the onSubmit="return false" statement. What it seems is happening (in IE6 anyway) is if I define a submit button (an input with a type of 'submit' and handle the onClick event, the onclick event will get called if I am in an edit field and press enter. When I simply changed the type to 'button' the call did not happen anymore.
    Brad Eck
    www.sitesdynamic.com

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by whammy
    No, if you have

    <form onsubmit="return false">

    Nothing will submit it, even enter, unless the user has javascript disabled - in which case you'd need to use server-side checking for redunancy anyway.
    Not true. Calling the submit() method of a form does not fire the onsubmit event handler.

    <input type="button" value="Submit" onclick="this.form.submit()" />

    Just to throw another log on the fire
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    That's why you should use form.onsubmit() instead of form.submit()...
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    form.onsubmit()?? Wha?

    do you mean

    form.onsubmit = functionPointer;

    ???

    That doesn't work anyway. There's really no difference between the HTML assigned and script-assigned event handlers. The submit() method for forms just plain doesn't fire onsubmit.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #9
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    No...

    <form onsubmit="statements">
    is the same as
    Code:
    form.onsubmit=function(){
        statements
    }
    as all other events are

    Why else would return true|false work?
    Last edited by liorean; 04-02-2003 at 07:40 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #10
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    my bad, I thought your "that's why" comment was for me, not bradeck. How arrogant of me

    Carry-on, fellas
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #11
    New Coder
    Join Date
    Mar 2003
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK - so going bak - we all agree on syntax and what SHOULD happen. What I am saying is (and agreeing) that while <form onsubmit="return false"> causes the form submission to fail, if I have an onclick() event in a submit button (i.e. <input type="submit"> it *will* get called for <enter> on a text field. We all know there is limited use for a form that won't submit - what I am doing (for reasons which are deep but clear) is setting the forms properties (action, target, etc) dynamically in the function called in the onclick and then calling submit. The prob is that if the input type is 'submit' - the onclick() handler gets called for <enter> on any text field - THAT seems odd to me. When I simply changed the input type to 'button' all my issues went away.
    Brad Eck
    www.sitesdynamic.com

  • #12
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Well, the thing is, you don't put onsubmit="return false;" in your form tag - you put onsubmit="return fnValidateForm(this);" in it.

    fnValidateForm then returns true or false depending on whether the form is filled out as you you want it or not.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #13
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's an example of that:
    Code:
    <script type="text/javascript">
    <!--
    
    	function validateForm(formObj)
    	{
    		if(formObj.textbox.value!="hello")
    		{
    			alert("you didn't say hello :(");
    			return false;
    		}
    		else
    		{
    			alert("Hello :)");
    			return true;
    		}
    	}
    
    //-->
    </script>
    
    <form onsubmit="return validateForm(this)" action="" method="get">
    	<input type="text" name="textbox" value="" />
    	<input type="submit" />
    </form>
    Last edited by brothercake; 04-02-2003 at 05:38 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #14
    New Coder
    Join Date
    Mar 2003
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK - let's start completely over so you get a hint of what I have to do - as you will see - doing anything in onsubmit will not resolve the issue.

    I have a page used multiple times - i.e. it is 100% dynamic and I have limited browser compatibilities to 6.0 and above (this is a targetted web page to ship with a product - not a website). Given that, I create a generic form on the page with few, if any, attributes. Then, when I need to set up form elements, I create them all on the fly using Javascript and then set up the calls appropriately to handle submission. Unfortunately, and despite my arguments about standards, my employer chooses to not want <ENTER> to ever work. However, submission still must work with a click of the submit button. Now, if I define the form attribute onsubmit to contain a call to my function, there is no way to return false if <ENTER> is pressed because there is no way to tell if <ENTER> was pressed. I have to stop it from functioning period. By setting up the 'return false', I tell the form nothing standard will submit this.

    However, the exception seems to be if I have a submit button (of type 'submit') with an onclick handler. In that case, the function gets called on <ENTER> in a text box. THIS IS WHAT SURPRISED ME. If I change the type to 'button' I no longer get the call on <ENTER> and only get it onclick - which is all I need.

    Does that clarify?
    Brad Eck
    www.sitesdynamic.com

  • #15
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <html>
    <head>
    
    <script type="text/javascript">
    
    function checkKey( e )
    {
    	var kc = ( e ) ? e.which : event.keyCode;
    	return ( kc != 13 );
    }
    
    </script>
    </head>
    <body>
    
    <form onkeypress="return checkKey()">
    <input type="text" name="" />
    <input type="submit" name="Submit" value="Submit" />
    </form>
    
    </body>
    </html>
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •