Skip to content
Advertisement

javascript form not validating fields

I’m trying to validate a form via javascript onSubmit, then run the php captcha verfication and email send action. The problem is that every time I try to check the fields, I can see just one of them highlited with my CSS classes (seems to be related to the ‘return false;’ which blocks me).

Anyone has a clue?

Here’s my HTML form code (you can see it working here: https://jsfiddle.net/xfmsLa95/2/ ):

JavaScript

And this is my validator.js file:

JavaScript

PHP is working.

I’m adding some pictures to refer this. This is what I get:

error

But I want to get something like this:

example

To validate field per field like this:

validation

Thanks in advance,

Advertisement

Answer

Try this in validator.js

JavaScript

What I’ve done here is declared a variable called isValid and set it to true, and whenever the user puts something invalid in the filed it will change isValid to false, and at the very end of the function, it will return isValid.

Let’s say you put a username to password wrong,

JavaScript

the above code runs, and isValid is now false. and when the function is called, it will return isValid, which is false.

Now, let’s say you put everything correctly, then isValid is never assigned false. Because isValid is by default true that means it will return true meaning everything is valid

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