Reputation: 5
I have a quick question that should actually be really simple but I haven’t been able to find it online through MANY searches. I have an HTML form and a submit button. In the form, I have an ‘onsubmit()’ event which does validation. How do I call a submit function after I return my onsubmit value if true do ‘thissubmit()’ I currently have a onsubmit function() If my function returns true then I call - document.forms["formsignin"].submit(); return true; That goes to my jQuery ‘$(myForm).on("submit", function () {}’ I want to do this in Javascript ONLY and NOT with the $(myform).on() jQuery way.
<form id="fsignin" onsubmit="return validateit();">
<button type=”submit”>Sign in</button>
</form>
Function validateit() {
do validation stuff if(….){document.forms["formsignin"].submit(); return true;
$(myForm).on("submit", function () {
if (validateit()) {
}
}
Upvotes: 0
Views: 347
Reputation: 2068
First, validate each condition on onSubmit()
function and use onclick
method in html button to call this function. If that condition is true then will go to next condition and so on. If all conditions are true then submit the form. If not then return false and exit the function.
onSubmit(){
if(!validateCondition1){
enter code here
return false;
}
if(!validateCondition2){
enter code here
return false;
}
....submit code.....
}
Upvotes: 0
Reputation: 43
Your formatting is not correct. You have a submit attribute and a submit event via jQuery. You only need one, or addEventListener for JS.
The name or id of your form is wrong.
You then wouldn't call submit, just return true to continue with submission.
Try to study different approaches in isolation. You need to understand them before, if desired, you consider combining them
Upvotes: 1