Skip to content
Advertisement

JavaScript form (ajax submission) with validation doing something funky

I am trying to do a form validation function with Ajax submission. For some reason the validation doesn’t work and when it submits, my server gets the empty fields (when I am testing for the validation) but it shows that it tried to post to the same page… I don’t know why.

Form:

JavaScript

My JS File:

JavaScript
JavaScript

Where I go when I submit (the form is at this URL): Where I go when I submit (the form is at this URL):

Any Idea what I should do? I am using node.js with express.js.

I added: e.preventDefault() to my submit event handler but now when I submit the form without filling in anything, I get this notification:

enter image description here

Advertisement

Answer

I changed your click handler:

  • default prevention,
  • validate() as variable so that it runs only once,

the errors declaration

  • empty string instead of undefined,

your validate() function

  • excluded button using :not(),
  • changed arrow function to ordinary anonymous function,
  • used this instead of element, which was only an index,
  • added a return to the last else,

and your email() function

  • sourced the email validation out to an own function.

I also deleted send(), as it was not used, declared the variables in sendForm() with var and added many semicolons -> maybe in your code one is already missing and you are hoping that the js error correction will add them automatically…

Finally i added the param showIcon to your Notify objects (that was the ‘undefined’ part 😉

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement