I have 5 text fields and I want to validate each text field before the cursor goes to the next text-field. Instead of mouse events or key events is there any effective methods that I should use ?
If you are using Javascript you can use onchange attribute of a textbox which will call a function when the text box looses focus.
If you are using JQuery (JQuery has lot of advantages over plain Javascript) you can make use of the "blur" function, which is called when textbox looses focus.
You can go through this
I made the the above changes like you both suggested.
But when I place the cursor in the second text-box without entering details in first text-box I get a alert box, after pressing the 'ok' button the cursor is still in second text-box but I want to make the cursor to come the first text-box because first text-box is not valid.
So, which way is better
1. Simply showing alert box on typing invalid input in each text-box and doing validation check once again for all text-boxes before submitting the form.
2. Making sure that user enter correct input in a text-box before he enters the second text-box.
Depends on your client's requirement.
In the past we have implemented both the scenarios mentioned by you for different clients.
But most of them went for the 1st approach.
Fewer clients went for the 2nd approach as user can become frustrated if not allowed to go anywhere on browser without valid input.
One more thing...in Javascript, comments are just like Java.
// for single line comment
/**/ for multiline comment.
<!-- --> this type of comment applies to HTML and XML.