I don't know whether I understood your problem properly.
However, when we have a form and a submit button ...then to stop submitting use the client side script of the same button. Put all your validation behind that button and return false in every loop when you don't want to submit. Returning false restrains submission in all cases.
Also, It would be better if you describe you problem with the entire code...still not clear..