Eni*_*ack 3 html javascript php forms validation
我希望将表单提交到使用后验证后PHP调用的文件.该形式给出如下:reg.phpJavaScriptHTML
HTML表格:
<form method="post">
<input type="text" id="username" name="username"></input>
<button type="button" onclick="val()">Sign Up</button>
</form>
Run Code Online (Sandbox Code Playgroud)
JavaScript验证
以下是JavaScript验证码:
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.
}
}
Run Code Online (Sandbox Code Playgroud)
只需添加action="reg.php"在<form>和返回VAL()函数onclick,也使type="submit"形成的提交,如:
<form method="post" action="reg.php">
<input type="text" id="username" name="username"></input>
<button type="submit" onclick="return val()">Sign Up</button>
</form>
Run Code Online (Sandbox Code Playgroud)
并且只需添加return true;在else这样的:
function val(){
var uname = document.getElementById('username').value;
if(!uname){
document.getElementById("error").innerHTML = "Enter a username";
return false;
}
else{
return true;
}
}
Run Code Online (Sandbox Code Playgroud)