'html-php form submission after validation through JavaScript [closed]

What I intend to do is : first validate a form and then process if some conditions are satisfied. I have pasted the prototype of my code below. I have cross checked for any minor/silly errors.

Code:

<form id = "myform" action = process.php method = 'post'>
//some form elements here
<input type="submit" value="Submit this form"onclick="validate();return false;" />
</form>

<script type="text/javascript">
function validate(){
if(validated) myform.submit();
else Alert(Some message); 
</script>
}


Solution 1:[1]

I think you would need to do something like this:

<form id="myform" action="process.php" method="post" onsubmit="return validate();">
//some form elements here
<input type="submit" value="Submit this form" />
</form>

<script type="text/javascript">
function validate(){
    ...
    if(!valid)
        alert('Error');
    return valid;
}
</script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kapa