If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Form validation on submit.

Hello everybody, I am new at this and I'd like to show you what I coded and what's my problem. I've made a registration form, and I've made some functions to see if the username is taken, if password is right, email, etc...

But I call those functions on blur and I'd like also to call them on submit.

I've looked for this on google and I found something that I tried to implement but it doesn't work. Now I'll show you my code.

What I've done here (or tried), is making a function for every field of the form, to test if what is written is correct or not. Then I call the events of each one in the event blur after I define all the functions.

And last one, I try to validate on submit, calling all the functions. But it seems that the functions doesn't return any value. For any reason unknown to me, the functions are made after the the "if sentence", or that's what I think.

I asked in other forum but someone explained me something I really didn't understand.

He told me this:

your code is wrong, your form validates each operation in the server with an AJAX request, who is async and it always return false, except if you use callbacks in chain of onreadystatechange.

I tried looking for that "onreadystatechange" but I didn't understand any explanation because I am new here.

When you submit the form AJAX requests are sent to the server. Let's say that your request is executing 200ms. But your js code will not wait while ajax request will finish. That means that your function that is calling in form submitting will be completed faster than ajax and will always return false.

When you send ajax request the XmlHttpRequest object is created. It has a property readyState that holds the state of the request. The onreadystatechange property stores a function that will be called when readyState property changes. You can read more about this here onreadystatechange