I need to validate the input textboxes using javascript

Viewed 33

All fields and values are set to be not equal to zero or empty. how do i make only one field to allow 0 using if statement. how do I mention in IF statement for only this particular textbox to allow the value of 0

3 Answers
if (input.value.lenght = 0){
     // invalidate form input
}else{
     // pass form input
}

If you have a general if statement which covers all the inputs and states that they can't have a empty or 0 value than add

if(... || inputThatAllowsZero.value == 0) {
 // ... part is your general if statement, you should just add || and beyond
 // and change inputThatAllowsZero variable to the input that you want to allow 0
 // || means OR in if statement. This statement works even if the inputs value is 0 
}

this in between of the if parenthesis.

Also check logical operators to understand more about this answer.

You can also share a minimal reproductible version of your code as an example for people to edit and share better fitting solutions.

Related