0

I'm trying to stop the submit if an error exists and go through the submission if no error exist.

This is just a section of the code. Regex works correctly, also the error texts were showing up too. The thing is that now I'm trying to control the submit proccess and due to an error (console is not showing up anything) the error text is not showing up and the submission is just going through even if an input error exists.

There is a mistake that I cannot figure out (No jquery please)

var sub = document.querySelector("#register input[type='submit']");//the submit botton
var err_text_1 = "First name must be filled out!";

sub.onsubmit = function(){
    if (first_name == null || first_name == "") {
        err_holder_1.innerHTML = err_text_1;
        return false;
    }
    else {
        err_holder_1.style.display = "none";
    }
}
tafa
  • 7,146
  • 3
  • 36
  • 40
Anamed
  • 109
  • 1
  • 1
  • 8

2 Answers2

0

To prevent submit add event in the function and return false like so:

sub.onsubmit = function (event) {

  // Will prevent submit
  event.preventDefault();
  return false;

};

Also select the form you are submitting not the button itself. So:

var sub = document.getElementById('ActionForm');

Hope this helps.

prototype
  • 3,303
  • 2
  • 27
  • 42
  • Return false and preventDefault has the same result. And either should only be used to stop submission e.g needs to be in the if that tests the validity: http://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false – mplungjan Jan 26 '16 at 06:16
0

You need to select the form like

var sub = document.querySelector("#register");//If register is your form id Not input[type='submit']
Zee
  • 8,420
  • 5
  • 36
  • 58