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.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    onSubmit after checking return false and still submit

    I am building a registration form and have some javascript that checks the forms user input. This is all activated when the submit button is pressed and the forms onSubmit event is called and runs a function.

    I have tested me functions and even when it returns false the form still submits?

    Im not sure how i can stop it submiting even if it returns false.

    Then onSubmit called this function:

    Code:
    	function subForm(theform){
    			if(checkForm(theform)==false){
    				alert("it is false");
    				return false;
    			}
    			alert("it is true");
    			return true;
    	}
    checkForm is working and is throwing alerts when certain criteria is not correct in the form, and then i will get an alert "it is false" so i know that the script is running until then, but the form will still submit?

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    how are you attaching that function to the onsubmit event?
    if it's inline, like <form onsubmit="subForm(this);"> then you'll need to change it to <form onsubmit="return subForm(this);">, as the contents of the attribute are essentially a function, and only there will returning false do what you want.

    If you're attaching it either with attachEvent/addEventListener or "foo.onsubmit=", then provide more code...
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah ok. So i need to return false with the onSubmit event to stop it from submiting. At present i am just doing: onSubmit="javascript: subForm(this)"

  • #4
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    change to code to:

    Code:
    	function subForm(theform){
    			if(checkForm(theform)==false){
    				alert("it is false");
    				return false;
    			}
                         else{
    			alert("it is true");
    			return true;
                           }
    	}
    because from your code the alert("it is true") and return true; will execute anyway.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Quote Originally Posted by AlwaysNew View Post
    change to code to:

    Code:
    	function subForm(theform){
    			if(checkForm(theform)==false){
    				alert("it is false");
    				return false;
    			}
                         else{
    			alert("it is true");
    			return true;
                           }
    	}
    because from your code the alert("it is true") and return true; will execute anyway.
    No it will not execute if the checkForm call returns false as it does return false after alerting "it is false".
    Gjay is correct. To make it simpler, you can call the checkForm() directly and no need for another method subForm.

    Code:
    <form onsubmit="return checkForm(this);">


  •  

    Posting Permissions

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