Since I have a function which can be done in few ways as usual, I was wondering which way is the best to achieve the form validation where I have only one input field, and I am showing a warning on keyup if the number of characters are in between 1 and 3 as well as enabling disabling submit button based on number of characters. Here is the fiddle. So I have two functions:
var myFunction = function () {
var input = document.getElementById("input");
var warning = document.getElementById("warning");
var button = document.getElementById("button");
if (input.value.length >= 1 && input.value.length <= 3){
warning.style.display = 'block';
}
else {
warning.style.display = 'none';
}
if(input.value.length > 3){
button.disabled = false;
}
else {
button.disabled = true;
}
}
Here I have two if else statements, so I was wondering what would be the cleanest way to achieve the same thing? The only thing I could come up with was this function:
var alternative = function () {
var input = document.getElementById("input");
var warning = document.getElementById("warning");
var button = document.getElementById("button");
warning.style.display = 'none';
button.disabled = true;
if (input.value.length >= 1 && input.value.length <= 3)
warning.style.display = 'block';
if(input.value.length > 3)
button.disabled = false;
}
But I am not sure what is the best approach performance wise, since in the second function I have states that will always be the same on calling the function and then changing them based on conditions and in the first one I first check the conditions and then manipulate the elements?