두 개의 텍스트 상자, 전화 번호 #inputPh
, 및 이메일, #inputEm
. 이 중 하나는 필수입니다. 다음 규칙을 사용하고 있습니다.
rules: {
inputPhone: {
required: {depends: function(){
if($("#inputEm").val()=="")//return value based on textbox1 status
return true;
else
return false;
}
},
isNum: true,
minlength: 10,
maxlength: 12
},
inputEmail: {
required: {depends: function(){
if($("#inputPh").val()=="")//return value based on textbox1 status
return true;
else
return false;
}
},
email: true
}
},
highlight: function(element) {
$(element).closest('.control-group').removeClass('success').addClass('error');
},
success: function(element) {
element.addClass('valid').closest('.control-group').removeClass('error').addClass('success');
}
유효성 검사는 제대로 작동하지만 양식이 빈 필드로 제출되면 두 텍스트 상자가 모두 오류 클래스로 강조된다는 것입니다. 나중에 텍스트 상자 중 하나에 유효한 값을 입력하면 해당 particulor 필드 만 성공 클래스를 가져오고 다른 하나는 양식을 제출할 수 있더라도 오류 클래스를 갖게됩니다.
jQuery Validator 함수를 통해 종속 요소에 액세스하거나 전달하려면 어떻게해야합니까?
대신, 사용 require_from_group
에 포함 방법 파일을 . 코드 주석에 따라 :additional-methods.js
/*
* Lets you say "at least X inputs that match selector Y must be filled."
*
* The end result is that neither of these inputs:
*
* <input class="productinfo" name="partnumber">
* <input class="productinfo" name="description">
*
* ...will validate unless at least one of them is filled.
*
* partnumber: {require_from_group: [1,".productinfo"]},
* description: {require_from_group: [1,".productinfo"]}
*
*/
다른 필드에 남아있는 오류 클래스에는 문제가 없습니다.
$(document).ready(function() {
$('#myform').validate({
rules: {
inputPhone: {
require_from_group: [1, '.mygroup'],
// isNum: true, // <-- no such rule in this plugin
minlength: 10,
maxlength: 12
},
inputEmail: {
require_from_group: [1, '.mygroup'],
email: true
}
}
});
});
데모 : http://jsfiddle.net/wWjWM/
선택적으로이 groups
옵션을 사용하여 두 필드의 메시지를 하나의 레이블로 통합 할 수 있습니다 . 그러나 두 필드의 다른 모든 규칙에도 적용되므로 바람직하지 않을 수 있습니다.
BTW : additional-methods.js
파일에는 minlength
및 대신 사용할 수 있는 다양한 전화 번호 규칙 이 있습니다 maxlength
. ( isNum
이 플러그인 과 같은 규칙도 없습니다 .라고 digits
합니다.)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다