form onsubmit=“return function()” but form still submitting and page reloading











up vote
0
down vote

favorite












I want a form to not submit and reload the page. Instead I want it to run the runTest() function.



I've tried the following:



<form id="dataForm" onsubmit="return runTest()">
<input type="text" id="text" placeholder="enter some text" required maxlength="10" pattern="^[a-z,A-Z]{1,10}$"><br />
<input type="text" id="number" placeholder="enter a number" required maxlength="10" pattern="d{10}"><br />
<input type="submit" id="submitTest">
</form>
<script src="../test/test2.js"></script>


and in ../test/test2.js:



function runTest() {...}


However, the form submits and the page reloads, and runTest() does not run.



Help appreciated.



From this answer, I've altered my runTest() function to be:



function runTest() {

var validate = true;
describe("Simple assert", function() {
it("foo != bar", function() {
assert('foo' != 'bar', 'foo is not bar');
});
it('should return true if field is valid', function(){
var isValidText = isValidField('text');
var isValidNumber = isValidField('number');
assert.equal(isValidText, true);
assert.equal(isValidNumber, true);
});
validate = false;
});
return validate;
}


runTest() has been browserfyd into test2.js



However, the form still submits, the page reloads, and runTest() does not run.










share|improve this question


























    up vote
    0
    down vote

    favorite












    I want a form to not submit and reload the page. Instead I want it to run the runTest() function.



    I've tried the following:



    <form id="dataForm" onsubmit="return runTest()">
    <input type="text" id="text" placeholder="enter some text" required maxlength="10" pattern="^[a-z,A-Z]{1,10}$"><br />
    <input type="text" id="number" placeholder="enter a number" required maxlength="10" pattern="d{10}"><br />
    <input type="submit" id="submitTest">
    </form>
    <script src="../test/test2.js"></script>


    and in ../test/test2.js:



    function runTest() {...}


    However, the form submits and the page reloads, and runTest() does not run.



    Help appreciated.



    From this answer, I've altered my runTest() function to be:



    function runTest() {

    var validate = true;
    describe("Simple assert", function() {
    it("foo != bar", function() {
    assert('foo' != 'bar', 'foo is not bar');
    });
    it('should return true if field is valid', function(){
    var isValidText = isValidField('text');
    var isValidNumber = isValidField('number');
    assert.equal(isValidText, true);
    assert.equal(isValidNumber, true);
    });
    validate = false;
    });
    return validate;
    }


    runTest() has been browserfyd into test2.js



    However, the form still submits, the page reloads, and runTest() does not run.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want a form to not submit and reload the page. Instead I want it to run the runTest() function.



      I've tried the following:



      <form id="dataForm" onsubmit="return runTest()">
      <input type="text" id="text" placeholder="enter some text" required maxlength="10" pattern="^[a-z,A-Z]{1,10}$"><br />
      <input type="text" id="number" placeholder="enter a number" required maxlength="10" pattern="d{10}"><br />
      <input type="submit" id="submitTest">
      </form>
      <script src="../test/test2.js"></script>


      and in ../test/test2.js:



      function runTest() {...}


      However, the form submits and the page reloads, and runTest() does not run.



      Help appreciated.



      From this answer, I've altered my runTest() function to be:



      function runTest() {

      var validate = true;
      describe("Simple assert", function() {
      it("foo != bar", function() {
      assert('foo' != 'bar', 'foo is not bar');
      });
      it('should return true if field is valid', function(){
      var isValidText = isValidField('text');
      var isValidNumber = isValidField('number');
      assert.equal(isValidText, true);
      assert.equal(isValidNumber, true);
      });
      validate = false;
      });
      return validate;
      }


      runTest() has been browserfyd into test2.js



      However, the form still submits, the page reloads, and runTest() does not run.










      share|improve this question













      I want a form to not submit and reload the page. Instead I want it to run the runTest() function.



      I've tried the following:



      <form id="dataForm" onsubmit="return runTest()">
      <input type="text" id="text" placeholder="enter some text" required maxlength="10" pattern="^[a-z,A-Z]{1,10}$"><br />
      <input type="text" id="number" placeholder="enter a number" required maxlength="10" pattern="d{10}"><br />
      <input type="submit" id="submitTest">
      </form>
      <script src="../test/test2.js"></script>


      and in ../test/test2.js:



      function runTest() {...}


      However, the form submits and the page reloads, and runTest() does not run.



      Help appreciated.



      From this answer, I've altered my runTest() function to be:



      function runTest() {

      var validate = true;
      describe("Simple assert", function() {
      it("foo != bar", function() {
      assert('foo' != 'bar', 'foo is not bar');
      });
      it('should return true if field is valid', function(){
      var isValidText = isValidField('text');
      var isValidNumber = isValidField('number');
      assert.equal(isValidText, true);
      assert.equal(isValidNumber, true);
      });
      validate = false;
      });
      return validate;
      }


      runTest() has been browserfyd into test2.js



      However, the form still submits, the page reloads, and runTest() does not run.







      javascript forms






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 6:58









      Steve

      45942862




      45942862
























          4 Answers
          4






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You can handle the submit event of the form programmatically with Javascript using the addEventListener function as below:






          document.getElementById("dataForm").addEventListener("submit", function(e){
          e.preventDefault() //to block page reload
          var validate = true;
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          validate = false;
          });
          return validate;
          })

          <form id="dataForm">
          <input type="text" id="text" placeholder="enter some text" required maxlength="10" pattern="^[a-z,A-Z]{1,10}$"><br />
          <input type="text" id="number" placeholder="enter a number" required maxlength="10" pattern="d{10}"><br />
          <input type="submit" id="submitTest">
          </form>








          share|improve this answer





















          • I can't bring runTest() into the .html file because it is typescript in an external file that has been browserfy-d into a javascript file.
            – Steve
            Nov 20 at 7:30












          • i never asked you to put the script in your .html ;).... Normally it should work in your browseryfied file
            – Steve Nosse
            Nov 20 at 7:42










          • OK. So does the document.getElementById("dataForm").addEventListener("submit", function(e) go inside runTest ? Because the rest of the code in your answer is. :)
            – Steve
            Nov 20 at 7:48










          • No.. Instead on calling runTest when the form is submitted, i handle the submit event with javascript; so i replaced the runTest declaration by document.getElementById(... ... I think it's more professional like that
            – Steve Nosse
            Nov 20 at 8:00










          • Okay. We're getting somewhere. The form does not submit and the page does not reload now. I'll need to work out why the asserts are not running now. Thanks for your help.
            – Steve
            Nov 20 at 8:04


















          up vote
          2
          down vote













          pass event in your html.



          onsubmit="return runTest(event)"


          and then call preventDefault in your function and return false at the end.



          function runTest(e) {
          e.preventDefault();
          .
          .
          .
          return false;
          }





          share|improve this answer























          • Thanks. Tried that and the form still submits / page reloads.
            – Steve
            Nov 20 at 7:19










          • @Steve also return false at the end of function.
            – Hadi290
            Nov 20 at 7:24










          • Tried that too, same thing happens. :)
            – Steve
            Nov 20 at 7:25


















          up vote
          1
          down vote













          Change the type of that button to button instead submit then submit your form using JavaScript like this



          document.getElementById("dataForm").submit(); 


          and remove the return part from you onclick attribute.






          share|improve this answer





















          • I tried that. I end up with this code, but the browser gets trapped in a loop.
            – Steve
            Nov 20 at 7:37












          • You are directly including this statement document.getElementById("dataForm").submit(); inside script element so when the page load your form automatically get submitted .
            – RopAli Munshi
            Nov 20 at 7:44










          • I see. So where do I put your code please?
            – Steve
            Nov 20 at 7:46










          • Right question would when to call that method and the simple answer is that when you want to submit the form if you want to submit your form after the runTest function completes then you should call that submit method at the end of runTest function.
            – RopAli Munshi
            Nov 20 at 7:50










          • OK. I see this in my code editor when I place it at the end of runTest.
            – Steve
            Nov 20 at 7:56


















          up vote
          0
          down vote













          Please try the below code,



          use e.preventDefault();
          dont send the response as a veriable
          use return false; instead of return validate;



          function runTest() {
          e.preventDefault();
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          return false;
          });

          }


          or



           function runTest() {

          var validate = 'true';
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          validate = 'false';
          });
          alert(validate)
          if(validate=='false'){
          return false;
          }
          }





          share|improve this answer























          • That didn't work unfortunately.
            – Steve
            Nov 20 at 7:28










          • please try my second ans, I add a code to alert the value of the variable validate. so you can easy to check
            – JIJOMON K.A
            Nov 20 at 7:29










          • Tried the second code, and no alert occurred?
            – Steve
            Nov 20 at 7:34










          • @Steve : Then there is an error in the above codes. please add an alert() in function top. and also check your script path is correct
            – JIJOMON K.A
            Nov 20 at 7:35












          • I've added an alert just inside runTest() and it doesn't fire. I have the path correct I believe.
            – Steve
            Nov 20 at 7:40













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387759%2fform-onsubmit-return-function-but-form-still-submitting-and-page-reloading%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          You can handle the submit event of the form programmatically with Javascript using the addEventListener function as below:






          document.getElementById("dataForm").addEventListener("submit", function(e){
          e.preventDefault() //to block page reload
          var validate = true;
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          validate = false;
          });
          return validate;
          })

          <form id="dataForm">
          <input type="text" id="text" placeholder="enter some text" required maxlength="10" pattern="^[a-z,A-Z]{1,10}$"><br />
          <input type="text" id="number" placeholder="enter a number" required maxlength="10" pattern="d{10}"><br />
          <input type="submit" id="submitTest">
          </form>








          share|improve this answer





















          • I can't bring runTest() into the .html file because it is typescript in an external file that has been browserfy-d into a javascript file.
            – Steve
            Nov 20 at 7:30












          • i never asked you to put the script in your .html ;).... Normally it should work in your browseryfied file
            – Steve Nosse
            Nov 20 at 7:42










          • OK. So does the document.getElementById("dataForm").addEventListener("submit", function(e) go inside runTest ? Because the rest of the code in your answer is. :)
            – Steve
            Nov 20 at 7:48










          • No.. Instead on calling runTest when the form is submitted, i handle the submit event with javascript; so i replaced the runTest declaration by document.getElementById(... ... I think it's more professional like that
            – Steve Nosse
            Nov 20 at 8:00










          • Okay. We're getting somewhere. The form does not submit and the page does not reload now. I'll need to work out why the asserts are not running now. Thanks for your help.
            – Steve
            Nov 20 at 8:04















          up vote
          0
          down vote



          accepted










          You can handle the submit event of the form programmatically with Javascript using the addEventListener function as below:






          document.getElementById("dataForm").addEventListener("submit", function(e){
          e.preventDefault() //to block page reload
          var validate = true;
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          validate = false;
          });
          return validate;
          })

          <form id="dataForm">
          <input type="text" id="text" placeholder="enter some text" required maxlength="10" pattern="^[a-z,A-Z]{1,10}$"><br />
          <input type="text" id="number" placeholder="enter a number" required maxlength="10" pattern="d{10}"><br />
          <input type="submit" id="submitTest">
          </form>








          share|improve this answer





















          • I can't bring runTest() into the .html file because it is typescript in an external file that has been browserfy-d into a javascript file.
            – Steve
            Nov 20 at 7:30












          • i never asked you to put the script in your .html ;).... Normally it should work in your browseryfied file
            – Steve Nosse
            Nov 20 at 7:42










          • OK. So does the document.getElementById("dataForm").addEventListener("submit", function(e) go inside runTest ? Because the rest of the code in your answer is. :)
            – Steve
            Nov 20 at 7:48










          • No.. Instead on calling runTest when the form is submitted, i handle the submit event with javascript; so i replaced the runTest declaration by document.getElementById(... ... I think it's more professional like that
            – Steve Nosse
            Nov 20 at 8:00










          • Okay. We're getting somewhere. The form does not submit and the page does not reload now. I'll need to work out why the asserts are not running now. Thanks for your help.
            – Steve
            Nov 20 at 8:04













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You can handle the submit event of the form programmatically with Javascript using the addEventListener function as below:






          document.getElementById("dataForm").addEventListener("submit", function(e){
          e.preventDefault() //to block page reload
          var validate = true;
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          validate = false;
          });
          return validate;
          })

          <form id="dataForm">
          <input type="text" id="text" placeholder="enter some text" required maxlength="10" pattern="^[a-z,A-Z]{1,10}$"><br />
          <input type="text" id="number" placeholder="enter a number" required maxlength="10" pattern="d{10}"><br />
          <input type="submit" id="submitTest">
          </form>








          share|improve this answer












          You can handle the submit event of the form programmatically with Javascript using the addEventListener function as below:






          document.getElementById("dataForm").addEventListener("submit", function(e){
          e.preventDefault() //to block page reload
          var validate = true;
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          validate = false;
          });
          return validate;
          })

          <form id="dataForm">
          <input type="text" id="text" placeholder="enter some text" required maxlength="10" pattern="^[a-z,A-Z]{1,10}$"><br />
          <input type="text" id="number" placeholder="enter a number" required maxlength="10" pattern="d{10}"><br />
          <input type="submit" id="submitTest">
          </form>








          document.getElementById("dataForm").addEventListener("submit", function(e){
          e.preventDefault() //to block page reload
          var validate = true;
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          validate = false;
          });
          return validate;
          })

          <form id="dataForm">
          <input type="text" id="text" placeholder="enter some text" required maxlength="10" pattern="^[a-z,A-Z]{1,10}$"><br />
          <input type="text" id="number" placeholder="enter a number" required maxlength="10" pattern="d{10}"><br />
          <input type="submit" id="submitTest">
          </form>





          document.getElementById("dataForm").addEventListener("submit", function(e){
          e.preventDefault() //to block page reload
          var validate = true;
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          validate = false;
          });
          return validate;
          })

          <form id="dataForm">
          <input type="text" id="text" placeholder="enter some text" required maxlength="10" pattern="^[a-z,A-Z]{1,10}$"><br />
          <input type="text" id="number" placeholder="enter a number" required maxlength="10" pattern="d{10}"><br />
          <input type="submit" id="submitTest">
          </form>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 7:28









          Steve Nosse

          217111




          217111












          • I can't bring runTest() into the .html file because it is typescript in an external file that has been browserfy-d into a javascript file.
            – Steve
            Nov 20 at 7:30












          • i never asked you to put the script in your .html ;).... Normally it should work in your browseryfied file
            – Steve Nosse
            Nov 20 at 7:42










          • OK. So does the document.getElementById("dataForm").addEventListener("submit", function(e) go inside runTest ? Because the rest of the code in your answer is. :)
            – Steve
            Nov 20 at 7:48










          • No.. Instead on calling runTest when the form is submitted, i handle the submit event with javascript; so i replaced the runTest declaration by document.getElementById(... ... I think it's more professional like that
            – Steve Nosse
            Nov 20 at 8:00










          • Okay. We're getting somewhere. The form does not submit and the page does not reload now. I'll need to work out why the asserts are not running now. Thanks for your help.
            – Steve
            Nov 20 at 8:04


















          • I can't bring runTest() into the .html file because it is typescript in an external file that has been browserfy-d into a javascript file.
            – Steve
            Nov 20 at 7:30












          • i never asked you to put the script in your .html ;).... Normally it should work in your browseryfied file
            – Steve Nosse
            Nov 20 at 7:42










          • OK. So does the document.getElementById("dataForm").addEventListener("submit", function(e) go inside runTest ? Because the rest of the code in your answer is. :)
            – Steve
            Nov 20 at 7:48










          • No.. Instead on calling runTest when the form is submitted, i handle the submit event with javascript; so i replaced the runTest declaration by document.getElementById(... ... I think it's more professional like that
            – Steve Nosse
            Nov 20 at 8:00










          • Okay. We're getting somewhere. The form does not submit and the page does not reload now. I'll need to work out why the asserts are not running now. Thanks for your help.
            – Steve
            Nov 20 at 8:04
















          I can't bring runTest() into the .html file because it is typescript in an external file that has been browserfy-d into a javascript file.
          – Steve
          Nov 20 at 7:30






          I can't bring runTest() into the .html file because it is typescript in an external file that has been browserfy-d into a javascript file.
          – Steve
          Nov 20 at 7:30














          i never asked you to put the script in your .html ;).... Normally it should work in your browseryfied file
          – Steve Nosse
          Nov 20 at 7:42




          i never asked you to put the script in your .html ;).... Normally it should work in your browseryfied file
          – Steve Nosse
          Nov 20 at 7:42












          OK. So does the document.getElementById("dataForm").addEventListener("submit", function(e) go inside runTest ? Because the rest of the code in your answer is. :)
          – Steve
          Nov 20 at 7:48




          OK. So does the document.getElementById("dataForm").addEventListener("submit", function(e) go inside runTest ? Because the rest of the code in your answer is. :)
          – Steve
          Nov 20 at 7:48












          No.. Instead on calling runTest when the form is submitted, i handle the submit event with javascript; so i replaced the runTest declaration by document.getElementById(... ... I think it's more professional like that
          – Steve Nosse
          Nov 20 at 8:00




          No.. Instead on calling runTest when the form is submitted, i handle the submit event with javascript; so i replaced the runTest declaration by document.getElementById(... ... I think it's more professional like that
          – Steve Nosse
          Nov 20 at 8:00












          Okay. We're getting somewhere. The form does not submit and the page does not reload now. I'll need to work out why the asserts are not running now. Thanks for your help.
          – Steve
          Nov 20 at 8:04




          Okay. We're getting somewhere. The form does not submit and the page does not reload now. I'll need to work out why the asserts are not running now. Thanks for your help.
          – Steve
          Nov 20 at 8:04












          up vote
          2
          down vote













          pass event in your html.



          onsubmit="return runTest(event)"


          and then call preventDefault in your function and return false at the end.



          function runTest(e) {
          e.preventDefault();
          .
          .
          .
          return false;
          }





          share|improve this answer























          • Thanks. Tried that and the form still submits / page reloads.
            – Steve
            Nov 20 at 7:19










          • @Steve also return false at the end of function.
            – Hadi290
            Nov 20 at 7:24










          • Tried that too, same thing happens. :)
            – Steve
            Nov 20 at 7:25















          up vote
          2
          down vote













          pass event in your html.



          onsubmit="return runTest(event)"


          and then call preventDefault in your function and return false at the end.



          function runTest(e) {
          e.preventDefault();
          .
          .
          .
          return false;
          }





          share|improve this answer























          • Thanks. Tried that and the form still submits / page reloads.
            – Steve
            Nov 20 at 7:19










          • @Steve also return false at the end of function.
            – Hadi290
            Nov 20 at 7:24










          • Tried that too, same thing happens. :)
            – Steve
            Nov 20 at 7:25













          up vote
          2
          down vote










          up vote
          2
          down vote









          pass event in your html.



          onsubmit="return runTest(event)"


          and then call preventDefault in your function and return false at the end.



          function runTest(e) {
          e.preventDefault();
          .
          .
          .
          return false;
          }





          share|improve this answer














          pass event in your html.



          onsubmit="return runTest(event)"


          and then call preventDefault in your function and return false at the end.



          function runTest(e) {
          e.preventDefault();
          .
          .
          .
          return false;
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 7:26

























          answered Nov 20 at 7:16









          Hadi290

          188214




          188214












          • Thanks. Tried that and the form still submits / page reloads.
            – Steve
            Nov 20 at 7:19










          • @Steve also return false at the end of function.
            – Hadi290
            Nov 20 at 7:24










          • Tried that too, same thing happens. :)
            – Steve
            Nov 20 at 7:25


















          • Thanks. Tried that and the form still submits / page reloads.
            – Steve
            Nov 20 at 7:19










          • @Steve also return false at the end of function.
            – Hadi290
            Nov 20 at 7:24










          • Tried that too, same thing happens. :)
            – Steve
            Nov 20 at 7:25
















          Thanks. Tried that and the form still submits / page reloads.
          – Steve
          Nov 20 at 7:19




          Thanks. Tried that and the form still submits / page reloads.
          – Steve
          Nov 20 at 7:19












          @Steve also return false at the end of function.
          – Hadi290
          Nov 20 at 7:24




          @Steve also return false at the end of function.
          – Hadi290
          Nov 20 at 7:24












          Tried that too, same thing happens. :)
          – Steve
          Nov 20 at 7:25




          Tried that too, same thing happens. :)
          – Steve
          Nov 20 at 7:25










          up vote
          1
          down vote













          Change the type of that button to button instead submit then submit your form using JavaScript like this



          document.getElementById("dataForm").submit(); 


          and remove the return part from you onclick attribute.






          share|improve this answer





















          • I tried that. I end up with this code, but the browser gets trapped in a loop.
            – Steve
            Nov 20 at 7:37












          • You are directly including this statement document.getElementById("dataForm").submit(); inside script element so when the page load your form automatically get submitted .
            – RopAli Munshi
            Nov 20 at 7:44










          • I see. So where do I put your code please?
            – Steve
            Nov 20 at 7:46










          • Right question would when to call that method and the simple answer is that when you want to submit the form if you want to submit your form after the runTest function completes then you should call that submit method at the end of runTest function.
            – RopAli Munshi
            Nov 20 at 7:50










          • OK. I see this in my code editor when I place it at the end of runTest.
            – Steve
            Nov 20 at 7:56















          up vote
          1
          down vote













          Change the type of that button to button instead submit then submit your form using JavaScript like this



          document.getElementById("dataForm").submit(); 


          and remove the return part from you onclick attribute.






          share|improve this answer





















          • I tried that. I end up with this code, but the browser gets trapped in a loop.
            – Steve
            Nov 20 at 7:37












          • You are directly including this statement document.getElementById("dataForm").submit(); inside script element so when the page load your form automatically get submitted .
            – RopAli Munshi
            Nov 20 at 7:44










          • I see. So where do I put your code please?
            – Steve
            Nov 20 at 7:46










          • Right question would when to call that method and the simple answer is that when you want to submit the form if you want to submit your form after the runTest function completes then you should call that submit method at the end of runTest function.
            – RopAli Munshi
            Nov 20 at 7:50










          • OK. I see this in my code editor when I place it at the end of runTest.
            – Steve
            Nov 20 at 7:56













          up vote
          1
          down vote










          up vote
          1
          down vote









          Change the type of that button to button instead submit then submit your form using JavaScript like this



          document.getElementById("dataForm").submit(); 


          and remove the return part from you onclick attribute.






          share|improve this answer












          Change the type of that button to button instead submit then submit your form using JavaScript like this



          document.getElementById("dataForm").submit(); 


          and remove the return part from you onclick attribute.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 7:26









          RopAli Munshi

          1418




          1418












          • I tried that. I end up with this code, but the browser gets trapped in a loop.
            – Steve
            Nov 20 at 7:37












          • You are directly including this statement document.getElementById("dataForm").submit(); inside script element so when the page load your form automatically get submitted .
            – RopAli Munshi
            Nov 20 at 7:44










          • I see. So where do I put your code please?
            – Steve
            Nov 20 at 7:46










          • Right question would when to call that method and the simple answer is that when you want to submit the form if you want to submit your form after the runTest function completes then you should call that submit method at the end of runTest function.
            – RopAli Munshi
            Nov 20 at 7:50










          • OK. I see this in my code editor when I place it at the end of runTest.
            – Steve
            Nov 20 at 7:56


















          • I tried that. I end up with this code, but the browser gets trapped in a loop.
            – Steve
            Nov 20 at 7:37












          • You are directly including this statement document.getElementById("dataForm").submit(); inside script element so when the page load your form automatically get submitted .
            – RopAli Munshi
            Nov 20 at 7:44










          • I see. So where do I put your code please?
            – Steve
            Nov 20 at 7:46










          • Right question would when to call that method and the simple answer is that when you want to submit the form if you want to submit your form after the runTest function completes then you should call that submit method at the end of runTest function.
            – RopAli Munshi
            Nov 20 at 7:50










          • OK. I see this in my code editor when I place it at the end of runTest.
            – Steve
            Nov 20 at 7:56
















          I tried that. I end up with this code, but the browser gets trapped in a loop.
          – Steve
          Nov 20 at 7:37






          I tried that. I end up with this code, but the browser gets trapped in a loop.
          – Steve
          Nov 20 at 7:37














          You are directly including this statement document.getElementById("dataForm").submit(); inside script element so when the page load your form automatically get submitted .
          – RopAli Munshi
          Nov 20 at 7:44




          You are directly including this statement document.getElementById("dataForm").submit(); inside script element so when the page load your form automatically get submitted .
          – RopAli Munshi
          Nov 20 at 7:44












          I see. So where do I put your code please?
          – Steve
          Nov 20 at 7:46




          I see. So where do I put your code please?
          – Steve
          Nov 20 at 7:46












          Right question would when to call that method and the simple answer is that when you want to submit the form if you want to submit your form after the runTest function completes then you should call that submit method at the end of runTest function.
          – RopAli Munshi
          Nov 20 at 7:50




          Right question would when to call that method and the simple answer is that when you want to submit the form if you want to submit your form after the runTest function completes then you should call that submit method at the end of runTest function.
          – RopAli Munshi
          Nov 20 at 7:50












          OK. I see this in my code editor when I place it at the end of runTest.
          – Steve
          Nov 20 at 7:56




          OK. I see this in my code editor when I place it at the end of runTest.
          – Steve
          Nov 20 at 7:56










          up vote
          0
          down vote













          Please try the below code,



          use e.preventDefault();
          dont send the response as a veriable
          use return false; instead of return validate;



          function runTest() {
          e.preventDefault();
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          return false;
          });

          }


          or



           function runTest() {

          var validate = 'true';
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          validate = 'false';
          });
          alert(validate)
          if(validate=='false'){
          return false;
          }
          }





          share|improve this answer























          • That didn't work unfortunately.
            – Steve
            Nov 20 at 7:28










          • please try my second ans, I add a code to alert the value of the variable validate. so you can easy to check
            – JIJOMON K.A
            Nov 20 at 7:29










          • Tried the second code, and no alert occurred?
            – Steve
            Nov 20 at 7:34










          • @Steve : Then there is an error in the above codes. please add an alert() in function top. and also check your script path is correct
            – JIJOMON K.A
            Nov 20 at 7:35












          • I've added an alert just inside runTest() and it doesn't fire. I have the path correct I believe.
            – Steve
            Nov 20 at 7:40

















          up vote
          0
          down vote













          Please try the below code,



          use e.preventDefault();
          dont send the response as a veriable
          use return false; instead of return validate;



          function runTest() {
          e.preventDefault();
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          return false;
          });

          }


          or



           function runTest() {

          var validate = 'true';
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          validate = 'false';
          });
          alert(validate)
          if(validate=='false'){
          return false;
          }
          }





          share|improve this answer























          • That didn't work unfortunately.
            – Steve
            Nov 20 at 7:28










          • please try my second ans, I add a code to alert the value of the variable validate. so you can easy to check
            – JIJOMON K.A
            Nov 20 at 7:29










          • Tried the second code, and no alert occurred?
            – Steve
            Nov 20 at 7:34










          • @Steve : Then there is an error in the above codes. please add an alert() in function top. and also check your script path is correct
            – JIJOMON K.A
            Nov 20 at 7:35












          • I've added an alert just inside runTest() and it doesn't fire. I have the path correct I believe.
            – Steve
            Nov 20 at 7:40















          up vote
          0
          down vote










          up vote
          0
          down vote









          Please try the below code,



          use e.preventDefault();
          dont send the response as a veriable
          use return false; instead of return validate;



          function runTest() {
          e.preventDefault();
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          return false;
          });

          }


          or



           function runTest() {

          var validate = 'true';
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          validate = 'false';
          });
          alert(validate)
          if(validate=='false'){
          return false;
          }
          }





          share|improve this answer














          Please try the below code,



          use e.preventDefault();
          dont send the response as a veriable
          use return false; instead of return validate;



          function runTest() {
          e.preventDefault();
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          return false;
          });

          }


          or



           function runTest() {

          var validate = 'true';
          describe("Simple assert", function() {
          it("foo != bar", function() {
          assert('foo' != 'bar', 'foo is not bar');
          });
          it('should return true if field is valid', function(){
          var isValidText = isValidField('text');
          var isValidNumber = isValidField('number');
          assert.equal(isValidText, true);
          assert.equal(isValidNumber, true);
          });
          validate = 'false';
          });
          alert(validate)
          if(validate=='false'){
          return false;
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 7:28

























          answered Nov 20 at 7:26









          JIJOMON K.A

          570119




          570119












          • That didn't work unfortunately.
            – Steve
            Nov 20 at 7:28










          • please try my second ans, I add a code to alert the value of the variable validate. so you can easy to check
            – JIJOMON K.A
            Nov 20 at 7:29










          • Tried the second code, and no alert occurred?
            – Steve
            Nov 20 at 7:34










          • @Steve : Then there is an error in the above codes. please add an alert() in function top. and also check your script path is correct
            – JIJOMON K.A
            Nov 20 at 7:35












          • I've added an alert just inside runTest() and it doesn't fire. I have the path correct I believe.
            – Steve
            Nov 20 at 7:40




















          • That didn't work unfortunately.
            – Steve
            Nov 20 at 7:28










          • please try my second ans, I add a code to alert the value of the variable validate. so you can easy to check
            – JIJOMON K.A
            Nov 20 at 7:29










          • Tried the second code, and no alert occurred?
            – Steve
            Nov 20 at 7:34










          • @Steve : Then there is an error in the above codes. please add an alert() in function top. and also check your script path is correct
            – JIJOMON K.A
            Nov 20 at 7:35












          • I've added an alert just inside runTest() and it doesn't fire. I have the path correct I believe.
            – Steve
            Nov 20 at 7:40


















          That didn't work unfortunately.
          – Steve
          Nov 20 at 7:28




          That didn't work unfortunately.
          – Steve
          Nov 20 at 7:28












          please try my second ans, I add a code to alert the value of the variable validate. so you can easy to check
          – JIJOMON K.A
          Nov 20 at 7:29




          please try my second ans, I add a code to alert the value of the variable validate. so you can easy to check
          – JIJOMON K.A
          Nov 20 at 7:29












          Tried the second code, and no alert occurred?
          – Steve
          Nov 20 at 7:34




          Tried the second code, and no alert occurred?
          – Steve
          Nov 20 at 7:34












          @Steve : Then there is an error in the above codes. please add an alert() in function top. and also check your script path is correct
          – JIJOMON K.A
          Nov 20 at 7:35






          @Steve : Then there is an error in the above codes. please add an alert() in function top. and also check your script path is correct
          – JIJOMON K.A
          Nov 20 at 7:35














          I've added an alert just inside runTest() and it doesn't fire. I have the path correct I believe.
          – Steve
          Nov 20 at 7:40






          I've added an alert just inside runTest() and it doesn't fire. I have the path correct I believe.
          – Steve
          Nov 20 at 7:40




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387759%2fform-onsubmit-return-function-but-form-still-submitting-and-page-reloading%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          To store a contact into the json file from server.js file using a class in NodeJS

          Redirect URL with Chrome Remote Debugging Android Devices

          Dieringhausen