How to check variable value is empty or not in javascript?

File name : index.php

<select id="name" name="name" class="form-control">
<option value="">--- Select ---</option>
<option value="1">Habib</option>
<option value="2">Kalam</option>
<option value="3">Sara</option>
<option value="4">Arham</option>
<option value="5">Mahtab</option>
<option value="6">Sana</option>
</select>

<button type="button" class="btn btn-primary" name="add_name" id="add_name">Add</button>

<script type="text/javascript">

$(document).ready(function(){

$("#add_name").click(function() {

var id = $('#name').val();
//var id = $('#name option:selected');
var id_length = id.length;
if((id == '' || id == null) || (id == 'undefined' || id_length == 0))
{
swal("error!", "Please select Name", "error");
}

if(id)
{
$.ajax({
url:"",
method:"POST",
data:{id:id},
success:function(data){
//window.location.href = "mypage.php?id="+id;
swal("success!", "Standards successfully added", "success");
$("#name").val('');

}
});
}




});
});

</script>

File name : index.php

<script>
var firstName;
var lastName = null;
// Try to get non existing DOM element
var comment = document.getElementById('comment');

console.log(firstName); // Print: undefined
console.log(lastName); // Print: null
console.log(comment); // Print: null

console.log(typeof firstName); // Print: undefined
console.log(typeof lastName); // Print: object
console.log(typeof comment); // Print: object

console.log(null == undefined) // Print: true
console.log(null === undefined) // Print: false

/* Since null == undefined is true, the following statements will catch both null and undefined */
if(firstName == null){
alert('Variable "firstName" is undefined.');
}
if(lastName == null){
alert('Variable "lastName" is null.');
}

/* Since null === undefined is false, the following statements will catch only null or undefined */
if(typeof comment === 'undefined') {
alert('Variable "comment" is undefined.');
} else if(comment === null){
alert('Variable "comment" is null.');
}
</script>

File name : index.php

<form name="frm" id="frm" method="POST" action="">
<input type="text" name="title" id="title" /><br/>
<input type="text" name="name" id="name" /><br/>
<select name="state" id="state">
<option value=""> ---select--- </option>
<option value="1"> Bihar </option>
<option value="2"> UP</option>
<option value="3"> Delhi</option>
</select>

<a href="#" class="btn btn-success" id="next" onclick=
"submit_details()" type="button" name="SubmitDocument">
Submit</a>
</form>

<script>
function submit_details() {
if ($('#title').val() == "" || $('#title').val() == null)
{
$("#title").focus();
alert("Please Enter Title ");
return false;
}
else if ($('#name').val() == "" || $('#name').val() == null)
{
$("#name").focus();
alert("Please Enter Name");
return false;
}
else if ($('#state').val() == "" || $('#state').val() == null || $('#state').val() == "undefined" || $('#state').val().length == 0)
{
$("#name").focus();
alert("Please Enter Name");
return false;
}
else {
document.frm.action="ittutorial.php"; document.basic_frm.method="post";
document.basic_frm.submit();
}

}

</script>





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here