1

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>
2
  • inputs should not have end </input> tags. Commented Apr 15, 2018 at 5:12
  • Did you try to console log inside function validation()? Commented Apr 15, 2018 at 6:42

1 Answer 1

1

Because in the validation() function when fieldLabel is empty then you will return false,so the version have no chance to execute,change your code as below:

   function validation() {
        var fieldLabel = document.getElementById("fieldLabel").value;
        var version = document.getElementById("version").value;
        var isValid = true;
            if (fieldLabel == "") {
            document.getElementById("fieldLabelError").innerHTML = "**Please enter Field label";
            isValid = false;//use a variable to set the result
        }
        if (version == "") {
                document.getElementById("versionError").innerHTML = "**Please enter version";
               isValid = false;
        }

        return isValid;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.