简体   繁体   English

实时验证自定义验证器

[英]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

http://jsfiddle.net/r5X6P/ http://jsfiddle.net/r5X6P/

There were several issues with your question and fiddle: 您的问题和小提琴有几个问题:

  • You included this link in the script section, which is the github page, not code. 您在脚本部分(即github页面)而非代码中包括了此链接
  • You can't hotlink to github, to do this you need to use rawgithub.com 您无法热链接到github,为此,您需要使用rawgithub.com
  • This validation is working only on the first text box, not the second 该验证仅在第一个文本框上有效,而在第二个文本框上无效
  • You are passing "1" as value to Custom, which is not being used 您正在将“ 1”作为值传递给“自定义”,而未使用
  • Your code don't include a <form> , so onlyOnSubmit will never trigger 您的代码不包含<form> ,因此onlyOnSubmit将永远不会触发
  • Your <input> tags aren't closed 您的<input>标记未关闭
  • You don't need to if (condition) { return true } else { return false } , just return (condition) 您不需要if (condition) { return true } else { return false } ,只需return (condition)
  • You don't need to pass the values as args, just acquire them inside the function, it's cleaner and works just as well 您不需要将值作为args传递,只需在函数内部获取它们,它就更干净并且可以正常工作
  • Don't use obscure names like txtTaco 不要使用像txtTaco这样晦涩的名称

Working with the solution i found some issues with livevalidation: 使用该解决方案,我发现livevalidation存在一些问题:

  • You need to use an undocumented parameter displayMessageWhenEmpty to validate when the field is empty 您需要使用未记录的参数displayMessageWhenEmpty来验证该字段为空的时间
  • There's a bug to hide the validMessage . 有一个错误可以隐藏有效validMessage According to comments on the source code, you need to set it to false to hide it, but this don't work. 根据对源代码的注释,您需要将其设置为false以隐藏它,但这是行不通的。 So I just set it to a space. 所以我只是将其设置为空格。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM