2

i have an array of input fields which will contain some information. I should check them if one of them are empty.

jQuery Code:

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

            var zorunluAlan = ["#YeniAd", "#YeniSoyad", "#YeniEposta", "#YeniEpostaOnay", "#YeniSifre", "#YeniSifreOnay"];

            for (i = 0; i < zorunluAlan.length; i++) {
                if (//What should i write here?) {
                    alert("Please fill all input fields!");
                    break;
                }
            }
    });
2
  • 2
    are you considering an input filled with only space(s) as empty? Commented Sep 11, 2013 at 14:40
  • 2
    yes, spaces won't be allowed. Commented Sep 11, 2013 at 14:46

7 Answers 7

3

Use,

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

            var zorunluAlan = ["#YeniAd", "#YeniSoyad", "#YeniEposta", "#YeniEpostaOnay", "#YeniSifre", "#YeniSifreOnay"];

            for (i = 0; i < zorunluAlan.length; i++) {
                if ($(zorunluAlan[i]).val().trim().length == 0) {
                    alert("Please fill all input fields!");
                    return false;
                }
            }
    });
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming you're using jQuery, you need to put !$(zorunluAlan[i]).val() as your condition.

Comments

2

You can do it in a cleaner way using jQuery filter function:

var zorunluAlan = ["#YeniAd", "#YeniSoyad", "#YeniEposta", "#YeniEpostaOnay", "#YeniSifre", "#YeniSifreOnay"];

var $empty = $(zorunluAlan.join(",")).filter(function() {
    return ($(this).val().length === 0);
});

if($empty.length > 0) {
   alert("Please fill all input fields!");
}

11 Comments

filter is a loop :p
Eh, not really. Because in your code there's no way to stop the loop when an empty input is found, that's a loss of efficiency.
@Kolink yap, cleaner... not faster. But I bet in the end he will not break the loop so he can show a error message for each input ;)
Well, it's people like you that are the reason why the internet is so slow. But on the other hand I guess I should be thanking you because you all make my site seem infinitely faster by comparison.
@Kolink i'm used to people with those kind of "points". Because of <1ms of performance loss you can say with certainty that "it's people like me that are the reason why the internet is so slow". I'm glad strangers know me so well.
|
0

It's really easy, even in Vanilla JS

document.getElementById('NewUserBtn').onclick = function() {
    var zorunluAlan = ["YeniAd","YeniSoyad","........"],
        l = zorunluAlan.length, i;
    for( i=0; i<l; i++) {
        if( !document.getElementById(zorunluAlan[i]).value) {
            alert("Please fill all input fields! ["+zorunluAlan[i]+" is empty]");
            return false;
        }
    }
    return true;
}

Then again, it's even easier in HTML:

<input type="text" name="YeniAd" REQUIRED />

The attribute doesn't have to be in uppercase, I just made it so to make it more obivous ;)

Comments

0
   $("#NewUserBtn").click(function () {

       var zorunluAlan = ["#YeniAd", "#YeniSoyad", "#YeniEposta", "#YeniEpostaOnay", "#YeniSifre", "#YeniSifreOnay"];

       for (i = 0; i < zorunluAlan.length; i++) {
           if (!$.trim($(zorunluAlan[i]).val()).length) {
           alert("Please fill all input fields!");
           break;
           }
       }
   });

Comments

0

An alternative method using each

var zorunluAlan = $("#YeniAd, #YeniSoyad, #YeniEposta, #YeniEpostaOnay, #YeniSifre, #YeniSifreOnay"),
        invalid = false;
    if(zorunluAlan.length) {
      zorunluAlan.each(function() {
        var thisValue = $(this).val();
        if(thisValue == "" || !thisValue){
          invalid = true;
        }    
      });
    }
    if(invalid){
      alert("Please fill all input fields!");
    }

Comments

0

Basic idea using jQuery to add an error class

$("#a,#b,#c")
    .removeClass("error")
    .filter( 
        function(){
            return !this.value.length;
        }
    )
   .addClass("error");

http://jsfiddle.net/m3WA8/

It could be simplified by using a class/attribute instead of the string of ids.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.