I have this problem:
my HTML code:
<form name="addUser" method="post" action="../src/process_register.php">
....
<button type="submit" onClick="formhash_register(this.form);">Submit</button>
</form>
my JavaScript code:
function formhash_register(form)
{
var username = form.inputUsername.value;
if(username == "")
return false;
else
form.submit();
}
Although my function returns false, you will go to another page, and thats not what I wanted. Is there way to cancel submit process when the submit button is pressed ?
asked Oct 1, 2013 at 19:43
Ivan Vulović
2,2246 gold badges29 silver badges44 bronze badges
2 Answers 2
Try this:
<button type="submit"
onClick="return formhash_register(this.form);">Submit</button>
answered Oct 1, 2013 at 19:45
Sparky
15.1k2 gold badges34 silver badges45 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
defrost
Just have in mind that javascript can be disabled, so preventing submission is not 100% bulletproof. Always validate user input on server too.
Change type to "button" which shouldn't automatically submit this form.
<button type="submit" onClick="formhash_register(this.form);">Submit</button>
Comments
default