One approach is the following:
window.onkeyup = function(e) {
// getting all the <input> elements with the class of 'C1':
var inputs = document.querySelectorAll('.C1'),
// variables for later use within the loop:
current, min, max, test;
// iterating over the <input> elements with a
// for loop:
for (var i = 0, len = inputs.length; i < len; i++) {
// caching the current <input> element:
current = inputs[i];
// getting the value of the min and max attributes,
// parsed as a number in base 10:
min = parseInt(current.min, 10);
max = parseInt(current.max, 10);
// testing whether the current value is
// equal to, or greater than, the min AND
// is equal to, or less than, the max:
isValid = current.value >= min && current.value <= max;
// if the current value is not the default value
// (so the user has made a change to the held value):
if (current.value !== current.defaultValue) {
// if the number is valid:
if (isValid) {
// we remove the 'invalid' class-name (if it's there):
current.classList.remove('invalid');
// we add the 'valid' class-name:
current.classList.add('valid');
} else {
current.classList.remove('valid');
current.classList.add('invalid');
}
}
}
document.getElementById('enterSign').style.display = "block";
document.getElementById('spacer').style.display = "none";
if (event.which == 13) {
saveitem();
}
};
window.onkeyup = function(e) {
var inputs = document.querySelectorAll('.C1'),
current, min, max, test;
for (var i = 0, len = inputs.length; i < len; i++) {
current = inputs[i];
min = parseInt(current.min, 10);
max = parseInt(current.max, 10);
isValid = current.value >= min && current.value <= max;
if (current.value !== current.defaultValue) {
if (isValid) {
current.classList.remove('invalid');
current.classList.add('valid');
} else {
current.classList.remove('valid');
current.classList.add('invalid');
}
}
}
document.getElementById('enterSign').style.display = "block";
document.getElementById('spacer').style.display = "none";
if (event.which == 13) {
saveitem();
}
};
.valid {
border-color: limegreen;
}
.invalid {
border-color: red;
}
<div id='nav'>
<label for='speed'> Speed
<br />
<input type='number' class='C1' id='speed' placeholder='1-10' max='10' min='1' />
</label>
<br />
<label for='Iron'> Iron
<br />
<input type='number' class='C1' id='Iron' placeholder='1-5' max='5' min='1' />
</label>
<br />
<label for='Ice'> Ice
<br />
<input type='number' class='C1' id='Ice' placeholder='1-5' max='5' min='1' />
</label>
<br />
<label for='Mass'> Mass
<br />
<input type='number' class='C1' id='Mass' placeholder='1-5' max='5' min='1' />
</label>
<br />
<label for='Diameter'> Diameter
<br />
<input type='number' class='C1' id='Diameter' placeholder='1-5' max='5' min='1' />
</label>
<br />
<p id='spacer'> </p>
<p id='enterSign' onclick='saveitem()'>Press Enter</p>
<button id='btnReset' onclick='resetPage()'>Reset</button>
</div>
JS Fiddle demo.
The above approach has the window 'listening' for the keyup interaction in order to provide the event-handling, which means that not only does that event have to propagate from the <input> element all the way through each and every ancestor element before the function will be called, it's also possible to accidentally call event.stopPropagation() on one of the parent elements which, depending on the event-propagation being stopped, might prevent the function from ever being called.
You could, of course, attach the event-listener to a closer common ancestor to the <input> elements, such as the parent <div>:
var commonAncestor = document.getElementById('nav');
commonAncestor.onkeyup = function(e) {
// ... all contents removed for brevity
};
var commonAncestor = document.getElementById('nav');
commonAncestor.onkeyup = function(e) {
var inputs = document.querySelectorAll('.C1'),
current, min, max, test;
for (var i = 0, len = inputs.length; i < len; i++) {
current = inputs[i];
min = parseInt(current.min, 10);
max = parseInt(current.max, 10);
isValid = current.value >= min && current.value <= max;
if (current.value !== current.defaultValue) {
if (isValid) {
current.classList.remove('invalid');
current.classList.add('valid');
} else {
current.classList.remove('valid');
current.classList.add('invalid');
}
}
}
document.getElementById('enterSign').style.display = "block";
document.getElementById('spacer').style.display = "none";
if (event.which == 13) {
saveitem();
}
};
.valid {
border-color: limegreen;
}
.invalid {
border-color: red;
}
<div id='nav'>
<label for='speed'> Speed
<br />
<input type='number' class='C1' id='speed' placeholder='1-10' max='10' min='1' />
</label>
<br />
<label for='Iron'> Iron
<br />
<input type='number' class='C1' id='Iron' placeholder='1-5' max='5' min='1' />
</label>
<br />
<label for='Ice'> Ice
<br />
<input type='number' class='C1' id='Ice' placeholder='1-5' max='5' min='1' />
</label>
<br />
<label for='Mass'> Mass
<br />
<input type='number' class='C1' id='Mass' placeholder='1-5' max='5' min='1' />
</label>
<br />
<label for='Diameter'> Diameter
<br />
<input type='number' class='C1' id='Diameter' placeholder='1-5' max='5' min='1' />
</label>
<br />
<p id='spacer'> </p>
<p id='enterSign' onclick='saveitem()'>Press Enter</p>
<button id='btnReset' onclick='resetPage()'>Reset</button>
</div>
JS Fiddle demo.
Or you could implement this functionality with CSS, using the :valid and :invalid selectors:
:valid {
border-color: green;
}
:invalid {
border-color: red;
}
:valid {
border-color: green;
}
:invalid {
border-color: red;
}
<div id='nav'>
<label for='speed'> Speed
<br />
<input type='number' class='C1' id='speed' placeholder='1-10' max='10' min='1' />
</label>
<br />
<label for='Iron'> Iron
<br />
<input type='number' class='C1' id='Iron' placeholder='1-5' max='5' min='1' />
</label>
<br />
<label for='Ice'> Ice
<br />
<input type='number' class='C1' id='Ice' placeholder='1-5' max='5' min='1' />
</label>
<br />
<label for='Mass'> Mass
<br />
<input type='number' class='C1' id='Mass' placeholder='1-5' max='5' min='1' />
</label>
<br />
<label for='Diameter'> Diameter
<br />
<input type='number' class='C1' id='Diameter' placeholder='1-5' max='5' min='1' />
</label>
<br />
<p id='spacer'> </p>
<p id='enterSign' onclick='saveitem()'>Press Enter</p>
<button id='btnReset' onclick='resetPage()'>Reset</button>
</div>
JS Fiddle demo.
There is, of course, the range selectors — :in-range and :out-of-range — which provide similar functionality as above:
:in-range {
border-color: green;
}
:out-of-range {
border-color: red;
}
:in-range {
border-color: green;
}
:out-of-range {
border-color: red;
}
<div id='nav'>
<label for='speed'> Speed
<br />
<input type='number' class='C1' id='speed' placeholder='1-10' max='10' min='1' />
</label>
<br />
<label for='Iron'> Iron
<br />
<input type='number' class='C1' id='Iron' placeholder='1-5' max='5' min='1' />
</label>
<br />
<label for='Ice'> Ice
<br />
<input type='number' class='C1' id='Ice' placeholder='1-5' max='5' min='1' />
</label>
<br />
<label for='Mass'> Mass
<br />
<input type='number' class='C1' id='Mass' placeholder='1-5' max='5' min='1' />
</label>
<br />
<label for='Diameter'> Diameter
<br />
<input type='number' class='C1' id='Diameter' placeholder='1-5' max='5' min='1' />
</label>
<br />
<p id='spacer'> </p>
<p id='enterSign' onclick='saveitem()'>Press Enter</p>
<button id='btnReset' onclick='resetPage()'>Reset</button>
</div>
JS Fiddle demo.
References: