I have a form with a submit button. I want to perform some checking before submitting the form. How do i cancel a submit after the user has clicked the button?
Printable View
I have a form with a submit button. I want to perform some checking before submitting the form. How do i cancel a submit after the user has clicked the button?
try to run the validation code thru:
<form method="POST" action="register.cgi" onSubmit="runsomething>
get it?
You can do it with javascript !
Code:<script language="javascript">
function myValidateRoutine(my_form){
//do my form validation
}
</script>
<form method="POST" action="register.cgi" onSubmit="myValidateRoutine(this.form); return false;">
Shouldn't that be:
<script language="JavaScript">
function Validate(frmName){
//Check data here
if(good_data){
//if the data is good return true
return true;
}
else
//no good
return false;
}//end function
</script>
<form METHOD = "post" onSubmit="return Validate(this);">
Wouldn't the way you have it written cancel it anyway?
Yes your right it would cancel it but the form would be submitted by javascript from within the function if the validation is ok.
When you submit the form using javascript it by-passes the onSubmit atrribute of the form.
Either way doesn't matter you just need to have a false value in there. Othewise it will try to go to the script in the action attribute of the form.
I like the way you did it though :D