0

The code below validates a form with two fields. When I click the submit button without any data the error messages would show which is working fine but if I input data after and click submit button the error message doesn't disappear.

<script>
function validateForm() {
    var valid = true;
    var x = document.forms["myForm"]["activityName"].value;
    if (x == "" || x == null) {
        document.getElementById("activityName").innerHTML = "Please Enter Activity Name";
        valid= false;
    }

    var r = document.forms["myForm"]["reporter"].value;
    if (r == "") {
        document.getElementById("reporter").innerHTML = "Please Enter Reporter";
    valid = false;
    }

    return valid;

}   
</script>
</head>
<body>
    <form action="#" method="post" name="myForm" onsubmit=" return validateForm()">
    <div>
       <label for="myActivityName">*Activity Name:</label>
       <input type="text"  name="activityName" value="" placeholder="Enter Activity Name"  />
       <p id="activityName"></p>
    </div><br>

    <div>
       <label for="reporter">*Reporter:</label>
       <input type="text"  name="reporter" value="" placeholder="Enter Reporter "  />
       <p id="reporter"></p>
    </div><br>      
    <input type="submit" value="Submit" >
    </form>
</body>

2 Answers 2

1

The other answer is right, but here is some code to back it up with. Notice that the innerHTML of both activityName and reporter get (re)set back to empty before the validation occurs:

function validateForm() {
    var valid = true;
    document.getElementById("activityName").innerHTML = "";
    document.getElementById("reporter").innerHTML = "";

    var x = document.forms["myForm"]["activityName"].value;
    if (x == "" || x == null) {
        document.getElementById("activityName").innerHTML = "Please Enter Activity Name";
        valid= false;
    }

    var r = document.forms["myForm"]["reporter"].value;
    if (r == "") {
        document.getElementById("reporter").innerHTML = "Please Enter Reporter";
        valid = false;
    }

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

2 Comments

Thank you very much sir but can I ask you one more thing do you know where I have to put the confirm dialog so after it checks the validation and I click the submit button it would ask if to accept the data or cancel it to make changes?
The explanation is too long for a comment but have a look here: javascripter.net/faq/confirm.htm
0

Your problem is you never "unvalidate" the form a.k.a. remove the previous validation errors. Before you return from validation, if there were no errors, just revert your validation checks. This will ensure it will "clean" your interface if nothing is wrong.

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.