admin管理员组文章数量:1277405
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;
}
}
});
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;
}
}
});
Share
Improve this question
asked Sep 11, 2013 at 14:34
AloneInTheDarkAloneInTheDark
9384 gold badges15 silver badges39 bronze badges
2
- 2 are you considering an input filled with only space(s) as empty? – A. Wolff Commented Sep 11, 2013 at 14:40
- 2 yes, spaces won't be allowed. – AloneInTheDark Commented Sep 11, 2013 at 14:46
7 Answers
Reset to default 3Use,
$("#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;
}
}
});
Assuming you're using jQuery, you need to put !$(zorunluAlan[i]).val()
as your condition.
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!");
}
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 ;)
$("#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;
}
}
});
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!");
}
Basic idea using jQuery to add an error class
$("#a,#b,#c")
.removeClass("error")
.filter(
function(){
return !this.value.length;
}
)
.addClass("error");
http://jsfiddle/m3WA8/
It could be simplified by using a class/attribute instead of the string of ids.
本文标签: javascriptChecking elements in array if empty in jQueryStack Overflow
版权声明:本文标题:javascript - Checking elements in array if empty in jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741268571a2368901.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论