Reputation: 8294
Below is my vanilla js validation code. It works fine, but I am trying to add one more parameter specific to my 'number
' input field. I would like it to only validate if the user has entered a number higher then 0
, so 0
will not suffice as a correct input. i tried nesting another if statement
within my validation function but continuously get errors in console and breaks correct working code.
function validateForm(){
// var number = document.getElementById("number");
var form = document.getElementById("form"), inputs = form.getElementsByTagName("input"), input = null, flag = true;
for(var i = 0, len = inputs.length; i < len; i++) {
input = inputs[i];
// if the value is not a valid number or it's less than or equal 0
if(isNaN(input.value) || +input.value <= 0) {
flag = false;
input.focus();
console.log("Please fill all the inputs (with numbers > 0)");
break;
}
}
return(flag);ß
}
ie. attempt does not work when nested.
if (parseInt(number) > 0 && !isNaN(number))) {
// console
}
else {
// console
}
Upvotes: 0
Views: 792
Reputation: 486
Given that your input has type="number"
, You can use this condition
if (input.type == 'number' && +input.value <= 0) {
// error, entered number less than or equal to 0
}
Upvotes: 1