I want to submit my form to a PHP
file called reg.php
after validating it using JavaScript
. The HTML
form is given below:
HTML Form:
<form method="post">
<input type="text" id="username" name="username"></input>
<button type="button" onclick="val()">Sign Up</button>
</form>
JavaScript Validation
The following is the JavaScript
validation code:
function val(){
var uname = document.getElementById('username').value;
if(!uname){
document.getElementById("error").innerHTML = "Enter a username";
return false;
}
else{
// I want to submit form if this else statement executes.
}
}
type="submit"
and putonclick="return val();"
then the form will submit automatically unless your function returnsfalse
(as it does when validation fails). Just addaction="reg.php"
to the form element.