Back again with another absolute beginner question. I’m validating a form, and I want the text input to be limited to more than 2 and less than 15 characters. I have tried:
function validateForm(formElement) {
var valid = true;
if (formElement.first_name.value.length < 2) return focusElement(formElement.first_name, 'Please enter a First Name that is more than 2 characters long.');
if (formElement.first_name.value.length > 15) return focusElement(formElement.first_name, 'Please enter a First Name that is less than 15 characters long.');
As well as
if (formElement.first_name.value.length < 2 && formElement.first_name.value.length > 15) return focusElement(formElement.first_name, 'Please enter a First Name that is more than 2 and less than 15 characters long.');
Both resulting in errors. How can I accomplish this?
There’s a logic issue here – the length can’t be less than 2 AND greater than 15. What you’re should be using is less than 2 OR greater than 15:
I’m also not sure what you’re using the
valid = truevariable for here