0

Basically what i'm doing is passing 8 form input values to a Javascript Ajax file, during this process i want to do an

if(in_array(all form values) == 'empty') {

Here is the current code i'm using

if($("#merchant").val()==='') {
        //prevent submit button to sending to the handler page
        event.preventDefault();
        //popup the alert
        $("#response").html("<br/><div class='alert alert-error'>Please enter a Merchant Name</div>");
        $("#response").slideDown('slow');
        slideout();
        $("#loading").fadeOut('slow');
}

But using this code i have to copy and paste it about 8 times for each field, which i would rather do an array check if possible.

Is this possible with Javascript ? if so how ?

jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
Curtis Crewe
  • 4,126
  • 5
  • 26
  • 31
  • 1
    possible duplicate of [array.contains(obj) in JavaScript](http://stackoverflow.com/questions/237104/array-containsobj-in-javascript) – Felix Kling Feb 05 '13 at 22:52
  • 1
    Take a look at [this question](http://stackoverflow.com/questions/784012/javascript-equivalent-of-phps-in-array) (also, possibly a dupe). – thordarson Feb 05 '13 at 22:54

3 Answers3

2

Iterate over the :inputs of the form like this, assuming your in a submit event and the form is this

$(':input', this).each(function(e) {
    if ($(this).val() === '') {
        // do your stuff
    }
});

You could also potentially attach your error message as a data-attribute and reference it like this

<input type="text" ... data-error-msg="Some Error Message"/>

$(this).data('error-msg');
jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
  • That wouldn't work because i'm not calling the values via a form, i'm calling them via the `id` attribute of the field. – Curtis Crewe Feb 05 '13 at 23:02
  • I'm aware... I'm saying you shouldn't be calling them by the `id` attribute... Gather them up in a different way, and then loop over them with `.each()` – jondavidjohn Feb 05 '13 at 23:04
0

This function works perfect

function in_array(item,arr) {
     for(p=0;p<arr.length;p++) if (item == arr[p]) return true;
     return false;
}

Use it like this

if(in_array("value", myarray))
{
   // Do something
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Roman Holzner
  • 5,738
  • 2
  • 21
  • 32
  • How would i go about checking for empty fields which hold no values ? because that's only checking items with a length of more than one ? – Curtis Crewe Feb 05 '13 at 22:55
  • You can allways check with if($(element).val() == "") Im not sure anymore if i understood your problem – Roman Holzner Feb 05 '13 at 22:59
  • That's what i'm doing already, but i want to do it all in 1 with a simple function, instead of calling the same `if($("#merchant").val()==='') {` about 8 times, i'd rather do a simple `if(in_array('',$array);` but i don't know how to it in javascript – Curtis Crewe Feb 05 '13 at 23:02
-1

This will only select elements that are empty:

$(".my-class:empty").each(function() {
    // Do things
});
Evan Hahn
  • 12,147
  • 9
  • 41
  • 59