[英]Live Validate Custom Validator
I'm following this example, http://livevalidation.com/documentation#ValidateCustom , to check if both fields are filled in. So if the first field is not blank the second one cannot be blank and vice versa. 我按照以下示例http://livevalidation.com/documentation#ValidateCustom进行检查,以检查两个字段是否都已填写。因此,如果第一个字段不为空,则第二个字段不能为空,反之亦然。
Can anyone figure out why this code isn't working? 谁能弄清楚为什么此代码不起作用?
var txtFirstName = new LiveValidation('txtTaco', {onlyOnSubmit: true } );
txtFirstName.add(
Validate.Custom( 1, { against: function(value,args){
if(args.first||args.last){
if(args.first&&args.last){
return true;
} else {
return false;
}
}
},
args: {
first:txtTaco.value,
last:lastName.value}
}) //end custom
); // end add
There were several issues with your question and fiddle: 您的问题和小提琴有几个问题:
<form>
, so onlyOnSubmit
will never trigger <form>
,因此onlyOnSubmit
将永远不会触发 <input>
tags aren't closed <input>
标记未关闭 if (condition) { return true } else { return false }
, just return (condition)
if (condition) { return true } else { return false }
,只需return (condition)
txtTaco
txtTaco
这样晦涩的名称 Working with the solution i found some issues with livevalidation: 使用该解决方案,我发现livevalidation存在一些问题:
displayMessageWhenEmpty
to validate when the field is empty displayMessageWhenEmpty
来验证该字段为空的时间 validMessage
. validMessage
。 According to comments on the source code, you need to set it to false to hide it, but this don't work. I did only one validator, for last name. 我只对名字做了一个验证器。 When you change the first name it calls the last name validator.
当您更改名字时,它将调用姓氏验证器。 The downside is that if you style the border color of the invalid field, only last name will change.
缺点是,如果您设置无效字段的边框颜色的样式,则只会更改姓氏。
Here's the final code: 这是最终代码:
var valLastName = new LiveValidation('txtLastName', {validMessage: " "});
function validateFunction() {
var first = txtFirstName.value;
var last = txtLastName.value;
if (first || last) // If either are present...
return (first && last); // ...both must be present
return true;
}
valLastName.add(Validate.Custom, {
against: validateFunction,
failureMessage: "Please enter both first and last name",
displayMessageWhenEmpty: true
});
// Force validation of txtLastName
txtFirstName.onkeyup = function() {txtLastName.onkeyup()};
And fiddle: http://jsfiddle.net/P7Cf3/ 和小提琴: http : //jsfiddle.net/P7Cf3/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.