I have an <input type="number />
that may or may not have min
or max
values set. If the user enters a number outside that range, I want to print an error message. I have the error handling figured out but I’m trying to find a "nicer" way to determine which error message to print.
if(!!min && !!max && (val < min || val > max)) {
print(`Enter a number between ${min} and ${max}`);
} else if(!!min && val < min) {
print(`Enter a number greater than ${min}`);
} else if(!!max && val > max) {
print(`Enter a number less than ${max}`);
}
Is there a better way to structure this if/else block?
2
Answers
how about this
OR
Slight variation when min/max are undefined when unset. This also avoids the 0 border case…