I'm doing some validation on a calculator form I'm building and I want to a show a 'Please fill this field' message if the user hasn't entered a figure into the input.
By default, the field has a '0' in it.
So I want to check if the field is empty or has the string '0'.
Should also mention that there is a script running that prevents the user from entering non-numeric characters.
This is part of a series of checks in if/else statements. For this case, I already have one condition (to pick up a previous choice the user has made).
So I have something like:
if ( calcChoice == 1 && $( "#input-loan" ).val() == "0" ) {
console.log("User input missing");
$( "#input_loan" ).addClass( "error" );
} else if ( calcChoice == 1 && $( "#input-pay" ).val() == "" ) {
console.log("User input missing");
$( "#input_loan" ).addClass( "error" );
} else if ( calcChoice == 2 && $( "#input-loan" ).val() == "0" ) {
console.log("User input missing");
$( "#input_loan" ).addClass( "error" );
} else if ( calcChoice == 2 && $( "#input-pay" ).val() == "" ) {
console.log("User input missing");
$( "#input_loan" ).addClass( "error" );
I'd like to streamline this a bit. What I'd really like is something like:
if ( calcChoice == 1 && $( "#input-loan" ).val() == "" || "0"; ) {
//do something;
}
Any ideas?
I've looked at the following related questions but haven't been able to work it out from their answers:
How do you check for an empty string in JavaScript?
JavaScript: Simple way to check if variable is equal to two or more values?