form.html file
<!DOCTYPE html>
<html lang="en">
<head>
<title>Form Validation in Javascript</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
<body>
<div id="form">
<h1>Registration Form</h1><br><br>
<form name="registerform" onsubmit="validation()">
<label>FirstName:</label>
<input type="text" name="firstname" placeholder="firstname"/>
<span class="error" id="firstnameerror">hello</span><br><br>
<label>LastName:</label>
<input type="text" name="lastname" placeholder="lastname"/><br><br>
<label>Email:</label>
<input type="email" name="email" placeholder="email"/><br><br>
<label>UserName:</label>
<input type="text" name="username" placeholder="username"/><br><br>
<label>Password:</label>
<input type="password" name="password" placeholder="*******"/><br><br>
<button type="submit" name="submit" >Register</button>
</form>
</div>
<script src="script.js"></script>
</body>
</head>
</html>
style.css
*{
margin:0px;
padding:0px;
}
body{
background-color:skyblue;
}
form{
margin-left:400px;
margin-top:100px;
}
h1{
text-align:center;
}
input{
width:40%;
height:35px;
}
label{
width:120px;
float:left;
height:35px;
font-size:25px;
}
button{
width:120px;
height:50px;
border-radius:6px;
background-color:green;
color:black;
}
脚本。 js文件
function validation(){
var firstname=document.forms["registerform"]["firstname"].value;
var lastname=document.forms["registerform"]["lastname"].value;
var email=document.forms["registerform"]["email"].value;
var username=document.forms["registerform"]["username"].value;
var pwd=document.forms["registerform"]["password"].value;
if(firstname=="")
{
document.getElementById("firstnameerror").innerHTML="Please enter the username";
}
}
我的问题是每当我点击表格验证功能的提交按钮被调用但是没有看到错误信息时输入用户名水平只有hello是否在span标签中为什么是标签我要显示自定义消息请输入如果用户在用户名字段填写空白时想要提交,但代码不能正常工作
答案 0 :(得分:2)
在按钮的点击上调用验证功能,如果有错误,请返回false
以防止表单提交。
function validation(){
var firstname=document.forms["registerform"]["firstname"].value;
var lastname=document.forms["registerform"]["lastname"].value;
var email=document.forms["registerform"]["email"].value;
var username=document.forms["registerform"]["username"].value;
var pwd=document.forms["registerform"]["password"].value;
if(firstname=="")
{
document.getElementById("firstnameerror").innerHTML="Please enter the username";
return false;
}
}
margin:0px;
padding:0px;
}
body{
background-color:skyblue;
}
form{
margin-left:400px;
margin-top:100px;
}
h1{
text-align:center;
}
input{
width:40%;
height:35px;
}
label{
width:120px;
float:left;
height:35px;
font-size:25px;
}
button{
width:120px;
height:50px;
border-radius:6px;
background-color:green;
color:black;
}
scri
<!DOCTYPE html>
<html lang="en">
<head>
<title>Form Validation in Javascript</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
<body>
<div id="form">
<h1>Registration Form</h1><br><br>
<form name="registerform" >
<label>FirstName:</label>
<input type="text" name="firstname" placeholder="firstname"/>
<span class="error" id="firstnameerror">hello</span><br><br>
<label>LastName:</label>
<input type="text" name="lastname" placeholder="lastname"/><br><br>
<label>Email:</label>
<input type="email" name="email" placeholder="email"/><br><br>
<label>UserName:</label>
<input type="text" name="username" placeholder="username"/><br><br>
<label>Password:</label>
<input type="password" name="password" placeholder="*******"/><br><br>
<button type="submit" name="submit" onclick="validation()">Register</button>
</form>
</div>
</body>
</head>
</html>