Using JavaScript I'm trying to put together some validation for my form. What I'm trying to achieve is that if one input field is left blank, then a value is automatically added to that field upon hitting submit.
I will start off by saying I attempted by using the answer from this question: If input value is blank, assign a value of “empty” with Javascript.
However, this did not work.
I then tried to use the following in my external .JS
file:
validateForm();
function validateForm() {
"use strict";
//POSITION VALIDATION
var numberOf = document.forms.themainform.numberOf.value;
if (numberOf === "") {
numberOf.value = "-5";
}
}
and I also attempted to use a variation of the other questions answer:
var numberOf = document.forms.themainform.getElementById('numberOf');
if(numberOf.value.length === 0) {
numberOf.value = "-5";
}
But as before, no luck. Below is the code to my HTML form:
<script>validateForm();</script>
<form action="" method="post" name="themainform" onsubmit="return validateForm()">
<table>
<tr><td>Number Of</td>
<td><input type="text" name="numberOf" id="numberOf" size="5" maxlength="5"/></td>
</tr>
</table>
<input type="submit" name="themainform" value="submit"/>
<hr/>
</form>
Any help would be greatly appreciated!