2013-09-25 62 views
-1

我使用jQuery验证插件和我有两个字段这样的:验证表单如果只有一两个字段不为空

<input id="nbextr" name="nbextr" class="form-control" type="text" title="Ex: 3" 
value="" tabindex="3" /> 
<input id="nbextr2" name="nbextr2" class="form-control" type="text" title="Ex: 3" value="" tabindex="4" /> 

我需要的,只有这两个领域中的一个不为空。

而我没有找到一个基本的解决方案。

+1

可能重复[jQuery验证 - 两个字段,只需填写一个](http://stackoverflow.com/questions/8137844/jquery-validation-two-fields-only-required-to-fill-in - 一) - 你不能看起来很硬 – mplungjan

+0

可能重复的[jQuery验证:验证一个字段,或两个字段的一对是必需](http://stackoverflow.com/questions/18993067/jquery-validate-validate -hat-one-field-or-both-of-a-pair-are-required) – Sparky

+0

你可以使用[additional_this.js'文件]中包含的'require_from_group'方法(http:// ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.js)。请参阅[此链接的用法](http://stackoverflow.com/questions/18993067/jquery-validate-validate-that-one-field-or-both-fields-of-a-pair-are-required)。 – Sparky

回答

-1

我不知道如何与验证做到这一点,但我的伎俩像这样:

$("#nbextr").blur(function() { 
    if($("#nbextr").val().length > 0){ 
    $("#nbextr2").val(""); 
    } 
}); 

与同为其他领域。

+0

你问过一个jQuery Validate解决方案,你的答案与此无关。请参阅:http://stackoverflow.com/questions/18993067/jquery-validate-validate-that-one-field-or-both-fields-of-a-pair-are-required – Sparky

+0

我知道,谢谢。但我需要一个解决方案,所以我这样做等待。您的链接用于验证至少一个字段。所以只有nbextr或nbextr2。就像一个单选按钮。 – Portekoi

相关问题