jQuery Validate -只允许填充四个字段中的两个字段-如何验证
jQuery Validate - Only two fields of four allowed to be filled - how to validate?
我有四个输入字段:
MaxWidth
MaxHeight
和
Width
Height
我想验证只输入了MaxWidth + MaxHeight
或Width + Height
。用户不能同时提交两个集合的值
任何想法?
我正在使用jquery.validate.min.js提供的。net mvc默认应用程序
这就是我需要验证的
<-- Scale a picture to one of the max attributes and keeping the aspect ratio --/>
<input type="text" name="cmdMaxWidth" /><br />
<input type="text" name="cmdMaxHeight" /><br />
<br />
<-- Scale a picture to the exact width and height attributes, still keeping the aspect ratio but addting whitespace for the rest --/>
<input type="text" name="cmdWidth" /><br />
<input type="text" name="cmdHeight" /><br />
我已经重写了我的表单,以反映Mohen在他回答的命令中描述的UI/UX设置。
但我会用"接受"的答案奖励Jayendra Patil,但我会"标记"Mohens的答案和建议
亲切的问候div ' T
您可以很容易地禁用其他输入当一个值。
例如,你有这两个输入:
<input id="width" placeholder="width"/>
<input id="maxwidth" placeholder="max width"/>
,当用户输入width时,不允许输入maxwidth。然后你可以这样做:
$('#width').change(function(){
if($(this).val() != ''){
$('#maxwidth').attr('disabled', 'disabled');
}
});
对于width和maxwidth
也可以这样做JsBin文件
你也可以尝试使用依赖验证。
添加一个方法来检查其他两个字段。
并使所需的依赖于另一个字段。
Demo - http://jsfiddle.net/b3RmV/
cmdMaxWidth字段示例(id硬编码,可以作为参数传递)-
jQuery.validator.addMethod("allowed", function(value, element) {
return !($('#cmdWidth').val().length > 0 || $('#cmdHeight').val().length > 0)
}, "Not allowed");
$(document).ready(function(){
$("#commentForm").validate(
{ rules: {
cmdMaxWidth: {
required: function(element){
return $('#cmdMaxHeight').val().length > 0;
},
allowed: true
}
}
}
);
});
相关文章:
- 在输入字段上有两个函数调用,一个在Blur上,一个不在Angular中
- 同一页面上两个不同控制器的Angularjs双向绑定字段
- 我可以在Dynamics CRM 2016的复合地址字段中添加两个选项字段吗
- 在两个多选字段之间移动选项
- 我如何获得这个javascript来计算两个监听器字段作为价格
- 如何使用jquery将两个字段组合为下拉菜单中的选项
- 比较两个输入字段
- Angularjs 通过选择不同选择框中的两个字段来填充选择框
- 从一个套接字发出两个事件
- MongoDB聚合将两个不同的字段合并为一个并获取计数
- 在 JavaScript 中计算两个输入字段值
- 使用Join,要求两个字段中的一个字段为非空
- 如何计算mongodb中两个集合中一个字段的不同值的数量
- jquery自动更新两个选择字段
- 谷歌地图API JS-放置两个组合字段的自动完成
- 设置分类法字段多个值
- 如何接收“隐藏字段”第二个jsp中的值
- 在Mongoose / MongoDB中创建多字段(2个以上)索引
- 必填字段两个字段之一
- 套接字.等待两个玩家都选择了答案的IO事件