I am using JavaScript in JSP page and trying to display errors in all fields if no value is provided in the input field.. but for some reason error message is displayed only for the the first field which is field label.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<title>Test</title>
</head>
<body>
<div class="container">
<br>
<h1 class="text-success text-center">Test Test</h1>
<div class="col-lg-8 m-auto d block">
<form action="#" onsubmit="return validation()" class="bg-light">
<div class="form-group">
<label>FieldLabel:</label> <input type="text" name="fieldLabel"
class="form-control" id="fieldLabel"></input> <span
id="fieldLabelError" class="text-danger"></span>
</div>
<div class="form-group">
<label>Version:</label> <input type="text" name="version"
class="form-control" id="version"></input> <span id="versionError"
class="text-danger"></span>
</div>
<input id="submit" name="submit" type="submit" value="Submit"
onclick="validation()">
</form>
</div>
</div>
<script type="text/javascript">
function validation() {
var fieldLabel = document.getElementById("fieldLabel").value;
var version = document.getElementById("version").value;
if (fieldLabel == "") {
document.getElementById("fieldLabelError").innerHTML = "**Please enter Field label";
return false;
}
if (version == "") {
document.getElementById("versionError").innerHTML = "**Please enter version";
return false;
}
return true;
}
</script>
</body>
</html>
inputs should not have end</input>tags.validation()?