Reputation: 2455
I have one validation that I need to perform. When a string is null
or undefined
or ""
I need to show a message like "unable to perform further steps". If I do have values I need to proceed with further validations. I tried below code, but it doesn't work.
function test(){
var value = undefined; // api returns undefined
if(value != "" || value != undefined || value != null){
alert("ID::: " + value);
//next validations
}else{
alert("Id is blank, we are unable to process the request!!!");
return false;
}
}
<button type="button"onclick="test()">Click</button>
Upvotes: 1
Views: 414
Reputation: 4694
Try this-
function test(){
var value1 = undefined; // If api returns undefined
var value2 = null; // If api returns null
var value3 = ""; // If api returns empty string
var value4 = 45; // If api returns any value
examineValue(value1);
examineValue(value2);
examineValue(value3);
examineValue(value4);
}
function examineValue(value) {
if(value === "" || value === undefined || value === null){
alert("Id is blank, we are unable to process the request!!!");
return false;
}
alert("ID::: " + value);
//next validations
}
<button type="button"onclick="test()">Click</button>
Upvotes: 1
Reputation: 182
Using !someVariable
converts to a boolean and handles those checks automatically
function test(){
var value = undefined; // api returns undefined
if(!value){
alert("Id is blank, we are unable to process the request!!!");
return false;
}
alert("ID::: " + value);
//next validations
}
<button type="button"onclick="test()">Click</button>
Upvotes: 1
Reputation: 11
function test(){
var value = undefined; // api returns undefined
if(value != "" && value != undefined && value != null){
alert("ID::: " + value);
//next validations
}else{
alert("Id is blank, we are unable to process the request!!!");
return false;
}
}
<button type="button"onclick="test()">Click</button>
Upvotes: 1