In the above two codes i have found some errors and common mistakes. Please go through below points.Code 1:
for writing logics.
2) I dont understand why you have written form action & window.location.reload at same time if form is
submitted correctly.Code 2:
2) Your are not handling anything if the form is wrong. So,use logic to stop submitting of form or use
return while calling validate function with using false or true as above in code 1.
Have a nice day