0

How is the "check" made for this to check if a textfield is empty?

Jquery:

$("#AddGoogleMap").click(function () {      
    if($('#googleMapCity').val()==null) {
        alert("You need to fill out a city");
    } else {
     // do something
    }
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
Kim
  • 1,128
  • 6
  • 21
  • 41

5 Answers5

3

First, remove the whitespace from the beginning and end of a string and then compare the value with the empty string, like:

if( $.trim($('#googleMapCity').val()) === '') {
     alert("You need to fill out a city");
} else {
 // do something
}
palaѕн
  • 72,112
  • 17
  • 116
  • 136
1
if($('#googleMapCity').val() === "") {
 alert("You need to fill out a city");
}
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
1

Could be done like this:

if($.trim($('#googleMapCity').val())==="")
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
1
 $("#AddGoogleMap").click(function () {      
    if($('#googleMapCity').val()==='') {
    alert('You need to fill out a city');

     } else {
     // do something
     }
klewis
  • 7,459
  • 15
  • 58
  • 102
1

Working Demo

$("#AddGoogleMap").click(function () {      
    if($('#googleMapCity').val() === "") {
        alert("You need to fill out a city");
     } else {
     // do something
     }
});
Rajender Joshi
  • 4,155
  • 1
  • 23
  • 39