I'm building a contact form and I need help with the jQuery validator.
我正在构建一个联系表单,我需要有关jQuery验证器的帮助。
function contactform() {
$("form #submit").on("click", function() {
$("form input").removeClass("error");
validator();
// 3rd action goes here
});
});
validator()
checks to see if any input is left empty, and if so it adds an error class to it:
validator()检查是否有任何输入为空,如果是,则为其添加错误类:
function validator() {
$("form input").each(function() {
var value = $(this).val();
if (value.length <= 0) {
$(this).addClass("error");
return false;
}
});
});
Now, for the 3rd action in contactform()
I want to say that if validator() = true
(i.e. there no inputs that are empty), then continue on to the next code.
现在,对于contactform()中的第3个动作,我想说如果validator()= true(即没有空的输入),那么继续下一个代码。
I can't seem to return the value of validator()
. Does anybody know the best way to do this?
我似乎无法返回validator()的值。有人知道最好的方法吗?
3 个解决方案
#1
8
Here is another solution using filter
method:
这是使用过滤方法的另一种解决方案
function validator() {
return $("form input").filter(function() {
return $.trim(this.value).length == 0;
}).addClass("error").length == 0;
});
function contactform() {
...
if (validator()) {
// it's OK
} else {
// there are errors
}
}
UPDATE: Awesomely updated with the help of @am_not_i_am. Thanks!
更新:在@am_not_i_am的帮助下进行了极大的更新。谢谢!
#2
4
The problem you seem to encounter is that you have a nested function and closure, which prevents you from returning the value directly.
您似乎遇到的问题是您有一个嵌套的函数和闭包,这会阻止您直接返回值。
Something like that should do the trick:
像这样的东西应该做的伎俩:
function validator() {
var result=true;
$("form input").each(function() {
var value = $(this).val();
if (value.length <= 0) {
$(this).addClass("error");
result = false;
}
});
return result;
});
#3
3
function validator() {
var result = true;
$("form input").removeClass("error");
$('form input').each(function() {
if(!$.trim(this.value)) {
$(this).addClass('.error');
result = false;
}
});
return result;
}
function contactform() {
$("form #submit").on("click", function() {
if(validator()) { // pass the validation
} else { // fail validation
}
// 3rd action goes here
});
});
#1
8
Here is another solution using filter
method:
这是使用过滤方法的另一种解决方案
function validator() {
return $("form input").filter(function() {
return $.trim(this.value).length == 0;
}).addClass("error").length == 0;
});
function contactform() {
...
if (validator()) {
// it's OK
} else {
// there are errors
}
}
UPDATE: Awesomely updated with the help of @am_not_i_am. Thanks!
更新:在@am_not_i_am的帮助下进行了极大的更新。谢谢!
#2
4
The problem you seem to encounter is that you have a nested function and closure, which prevents you from returning the value directly.
您似乎遇到的问题是您有一个嵌套的函数和闭包,这会阻止您直接返回值。
Something like that should do the trick:
像这样的东西应该做的伎俩:
function validator() {
var result=true;
$("form input").each(function() {
var value = $(this).val();
if (value.length <= 0) {
$(this).addClass("error");
result = false;
}
});
return result;
});
#3
3
function validator() {
var result = true;
$("form input").removeClass("error");
$('form input').each(function() {
if(!$.trim(this.value)) {
$(this).addClass('.error');
result = false;
}
});
return result;
}
function contactform() {
$("form #submit").on("click", function() {
if(validator()) { // pass the validation
} else { // fail validation
}
// 3rd action goes here
});
});