简体   繁体   中英

Unable to perform validation using null || undefined || “” values

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>

 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>

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM