Reputation: 8366
So I have this html:
<form action="insert.php" method="post" onsubmit="return validateForm(this)">
<label for="FUP start date">FUP start date:</label>
<input type="text" name="inputField" id="inputField"/>
</br>
</br>
<label for="FUP end date">FUP end date: </label>
<input type="text" name="inputField2" id="inputField2" />
</br>
</br>
<label for="Allowed traffic">Allowed traffic:</label>
<input type="text" name="Allowed_traffic" id="Allowed_traffic"/>
</br>
</br>
<label for="Frequency">Frequency: </label>
<input type="text" name="Frequency" id="Frequency" />
</br>
</br>
<input type="submit" value="submit">
</form>
And this javascript for password (Parola):
<script>
function validateForm(formElement) {
if (formElement.Allowed_traffic.length < 5) {
alert('aaaPlease enter a password that is at least 5 characters long');
return false;
}
if (formElement.Allowed_traffic.length > 10) {
alert('Please enter a password that is less than 10 characters long');
return false;
}
}
</script>
What am I doing wrong? I want to check on submit that the password has between 5 and 10 characters. Thank you!
Upvotes: 0
Views: 109
Reputation: 19619
Try to use
var Allowed_traffic = document.getElementById("Allowed_traffic");
instead of
formElement.Allowed_traffic
Upvotes: 0
Reputation: 38345
You're currently looking at the length
property of the <input>
element itself (which probably doesn't exist). What you want to be looking at is the length
property of the value of the <input>
element, so do the following:
if (formElement.Allowed_traffic.value.length < 5) {
alert('aaaPlease enter a password that is at least 5 characters long');
return false;
}
else if (formElement.Allowed_traffic.value.length > 10) {
alert('Please enter a password that is less than 10 characters long');
return false;
}
Note that I've changed it to an if ... else if
because if the first condition is true then the second one can't be.
Upvotes: 0