Why does calling this Javascript function submit my form?

hsbsid

I have a form validation script. Unless all conditions are met, the ambiguous function associated with form.onclick returns false (form doesn't submit). I have another function which upon error will append an error message to the errorLog div.

  function addError (msg) {
    msg = document.createTextNode(msg);
    br = document.createElement("br");
    errorLog.append(msg);
    errorLog.append(br);
    alert("yes");
  }

If an error is found, calling this function submits the form. The script doesnt even enter the function, it simply submits the data and refreshes. I realize there is an error in the function, but i cant seem to figure it out.

edit: more of my code

  form.onsubmit = function () 
  {
    if (nameCheck(fname, lname))
      return true;

    return false; 
  };

function that checks if "name" field is valid:

function nameCheck (fname,lname)
{
  if (("/\d/").test(fname) || ("/\d/").test(lname))
  {
    addError('Your name cannot contain numbers.');
    valid = false;
  }
}
user4639281

You're not stopping the form from submitting.

Use e.preventDefault() to temporarily stop the form from submitting, then if the form is correct use this.submit() to finish submitting the form

form.onsubmit = function (e) {
    e.preventDefault();
    if (nameCheck(fname, lname)) {
        this.submit();
        return true;
    }
    return false; 
};

Also, nameCheck will never resolve true, as it doesnt return anything. It should look like this

function nameCheck (fname,lname) {
  if (("/\d/").test(fname) || ("/\d/").test(lname)) {
    addError('Your name cannot contain numbers.');
    return false;
  }
  return true;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does my form not submit when the submit button is named "submit"?

From Dev

Why does my form submit and ignore validation?

From Dev

JavaScript: Why won't my form submit?

From Dev

Why does calling a JS function from onsubmit clear my form and console?

From Dev

Why does form still submit when you have javascript disabled?

From Dev

Why does my onclick property not run the function upon submit?

From Dev

JavaScript: Form onSubmit does not work when function is called 'submit'

From Dev

Calling Controller Function on Form Submit Laravel

From Dev

Why does my tail calling function not pause and flush output?

From Dev

Why does my _IEFormSubmit not submit

From Dev

Why does my Javascript trim Function not work?

From Dev

Call javascript function on submit form

From Dev

Javascript Form Validation Submit function

From Dev

Javascript function to submit parametric form

From Dev

Run JavaScript function on form submit

From Dev

Why is my form's submit button not working?

From Dev

Why does my form not get submitted when I press the submit button?

From Dev

Why does my dynamic javascript highlight form break?

From Dev

jQuery form submit does not work after calling $('#idname').html("");

From Dev

JavaScript added HTML Form does not submit

From Dev

Calling Classic ASP Function when Form Submit Button is clicked

From Dev

Issue with HTML form submit button not calling Google script function

From Dev

Simulate post form submit with a function call with javascript

From Dev

before form submit call function javascript

From Dev

Submit form field values to a Javascript function

From Dev

before form submit call function javascript

From Dev

Why does my no-submit (HtmlButton) still submit?

From Dev

HTML form submit not calling Javascript "onsubmit" handler in Wordpress post

From Dev

Bootstrap Form - Why is my submit button crowding my input?

Related Related

  1. 1

    Why does my form not submit when the submit button is named "submit"?

  2. 2

    Why does my form submit and ignore validation?

  3. 3

    JavaScript: Why won't my form submit?

  4. 4

    Why does calling a JS function from onsubmit clear my form and console?

  5. 5

    Why does form still submit when you have javascript disabled?

  6. 6

    Why does my onclick property not run the function upon submit?

  7. 7

    JavaScript: Form onSubmit does not work when function is called 'submit'

  8. 8

    Calling Controller Function on Form Submit Laravel

  9. 9

    Why does my tail calling function not pause and flush output?

  10. 10

    Why does my _IEFormSubmit not submit

  11. 11

    Why does my Javascript trim Function not work?

  12. 12

    Call javascript function on submit form

  13. 13

    Javascript Form Validation Submit function

  14. 14

    Javascript function to submit parametric form

  15. 15

    Run JavaScript function on form submit

  16. 16

    Why is my form's submit button not working?

  17. 17

    Why does my form not get submitted when I press the submit button?

  18. 18

    Why does my dynamic javascript highlight form break?

  19. 19

    jQuery form submit does not work after calling $('#idname').html("");

  20. 20

    JavaScript added HTML Form does not submit

  21. 21

    Calling Classic ASP Function when Form Submit Button is clicked

  22. 22

    Issue with HTML form submit button not calling Google script function

  23. 23

    Simulate post form submit with a function call with javascript

  24. 24

    before form submit call function javascript

  25. 25

    Submit form field values to a Javascript function

  26. 26

    before form submit call function javascript

  27. 27

    Why does my no-submit (HtmlButton) still submit?

  28. 28

    HTML form submit not calling Javascript "onsubmit" handler in Wordpress post

  29. 29

    Bootstrap Form - Why is my submit button crowding my input?

HotTag

Archive