I'm using JavaScript to validate a form field. Basically, if the form is submitted and the default value of the field is not altered, a text node with an error message will be generated. Problem is if the submit button is clicked over and over, the error message will appear just as many times as the button is clicked,
ie. Incorrect information.Incorrect information.Incorrect information.Incorrect information.
How do I make the generated message appear only on the first button submit. I tried this but it
JavaScript:
window.onload = function(){
'use strict';
var errorMsg = 'Incorrect information.';
document.getElementById('btn').onclick = function(){
if(document.getElementById('btn').value == document.getElementById('btn').defaultValue){
var msg = document.createTextNode(msg);
document.getElementById('btn').parentNode.appendChild(msg);
/* This if statement didn't work
if(msg){
document.getElementById('btn').parentNode.replaceChild(msg);
}*/
}
};
};
html:
<body>
<form action="#" method="POST" id="theForm">
<div><input type="text" name="firstName" id="firstName" value="First Name"/></div>
<input type="button" name="btn" id="btn" value="Submit Info"/>
</form>
<script src="js/validate.js"></script>
</body>
</html>