In which cases do an input field value equals '', null or undefined.
I'm trying to check empty fields
if($('#cur').val() == 'undefined' || $('#cur').val() == '' || $('#cur').val() == 'null'){
alert('Blank');
}
The value of an input element will always be a string. You can safely just compare to the empty string:
if($("#cur").val() === "") {
alert("Blank");
}
You are currently checking for the string literals "undefined" and "null". That would only be the case if the value of the input was actually the string "undefined" or "null". For example:
<!--This would be blank according to your original test-->
<input type="text" value="undefined">
It may be worth noting that if the input contains blank spaces but nothing else, and you want to count that as empty, the above will not suffice. You can use the jQuery trim function to remove leading/trailing white space from a string:
if($.trim($("#cur").val()) === "") {
alert("Blank");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With