0

I am trying to do this: if $('#name') is empty() alert ("Enter valid input") I am guessing I do something like this?

if $('#name') == empty() { 
alert("enter valid input");
}

I want to use a function so I can get used to reusing the same function over and over. I realize I could just type it in individually without using the switch function. Articles like this: Check if inputs are empty using jQuery seem to not use the switch or function method.

function empty(e){
    switch (e) {
    case "":
    case 0:
    case "0":
    case null:
    case false:
    case typeof this == "undefined":
        return true
    default: return false 

    }
    }

</script>

 $(document).on('click','#add_btn',function (){
           if $('#name') 
         $.ajax({
                type:'POST',
                url:'add.php',
                //dataType: "json",
                data: {
                    "name": $('#name').val(),
                    "coffee": $('#coffee').val(),
                }, 
                success: function(data){ 
                    alert(data);
                    if (data=='ADD_OK') {
                      location.reload();
                    } else {
                         alert('something wrong');
                    }
                      }
                 })
            });
    </script>
chess master1
  • 55
  • 1
  • 9

3 Answers3

0

Okay, let me help you to clean a little bit your mess.

First of all let's rewrite the empty function to better work and better look:

function empty(value) {
   return value === null || value === "" || value === undefined || (value.isArray ? value.length == 0 : false)
}

Now let's assume that you have two inputs and you want to check their values:

<input id="input1" type="text" />
<input id="input2" type="text" />

Now by using jQuery you select those inputs and send their values to the empty function:

var ids = ['input1' , 'input2'];
for (let id of ids) {
   if (empty($('#' + id).val())) {
      alert("The input: " + id + " is empty!");
   }
}

And that is pretty much all, a reusable function to check if a value is empty

Osakr
  • 1,027
  • 1
  • 13
  • 26
  • ok thx for your time posted as answer. quick question, should it not return true? because if "" happened, empty should == true – chess master1 Dec 17 '18 at 17:15
  • It returns true in case the value is an empty string, a null value, undefined or in case the value is an array if the array is empty ( its length == 0 ) true is returned. – Osakr Dec 17 '18 at 17:18
0

Here is the full length solution thanks to those who posted:

function empty(value) {
   return value === null || value === "" || value === undefined || (value.isArray ? value.length == 0 : false)
}
</script>

<script type="text/javascript"> 

        $(document).on('click','#add_btn',function (){
            var ids = ['name'];
for (let id of ids) {
    if (empty($('#name').val())) {
    alert("Input: "  +id+ " is not a valid input");
  }
   if (!empty($('#name').val())) {
         $.ajax({
                type:'POST',
                url:'add.php',
                //dataType: "json",
                data: {
                    "name": $('#name').val(),
                    "coffee": $('#coffee').val(),
                }, 
                success: function(data){ 
                    alert(data);
                    if (data=='ADD_OK') {
                      location.reload();
                    } else {
                         alert('something wrong');
                    }
                      }
                 })
   } }
            });
    </script>
chess master1
  • 55
  • 1
  • 9
  • There is no really need for two ifs. The second if ( `if (!empty($('#name').val()))`) can be replaced with an else in the first if – Osakr Jan 02 '19 at 10:15
-1
if ($('#name').val() === undefined || $('#name').val().length == 0){ 
   alert("enter valid input");
}

Assuming that you input $('#name') is an html input. The first condition check if the input is here

Murelh
  • 3
  • 4