简体   繁体   English

jQuery验证引擎:无法创建自定义函数

[英]jQuery Validation Engine: Trouble Creating a Custom Function

I've tried both the custom[function_name] and funcCall[methodName] methods listed the the documentation and I can't seem to get either to work. 我已经尝试了文档中列出的自定义[function_name]funcCall [methodName]方法,但我似乎无法工作。

my custom function looks like this: 我的自定义函数如下所示:

function einTest(field, rules, i, options) {
        if (field != null) {
            var x = field.toString();
            if (x.length != 5) {
                return options.allrules.ein.alertText2;
            }
        }
        else {
            return options.allrules.ein.alertText;
        }
    }

and I've created the following in the vaildationEngine-en.js file: 我在vaildationEngine-en.js文件中创建了以下内容:

"ein": {
                "alertText": "Not a number.",
                "alertText2": "Must be a 5 digit number."
    },

and I'm trying to use it on the following html field: 我正在尝试在以下html字段中使用它:

<tr>
<td><b>Associate Number (EIN):</b></td>
<td><input type="text" id="EIN" name="EIN" class="validate[required,funcCall[einTest]]" data-prompt-position="inline" /></td>
</tr>

but nothing seems to work... the einTest function is not firing. 但似乎没有任何作用...... einTest功能没有触发。

Any clues? 有线索吗?

jsFiddle Demo

More than likely you were not properly exposing the function. 很可能你没有正确地暴露这个功能。 The function needs to be scoped to be called in the global scope. 该函数需要作用域以在全局范围内调用。 "Validates a field using a third party function call" documentation , basically it means it looks for it to be scoped alongside window . “使用第三方函数调用验证字段” 文档 ,基本上它意味着它寻找它与window一起作用域。

So the first thing to do is make sure your function is exposed. 所以要做的第一件事就是确保你的功能暴露出来。

window.einTest = function(field, rules, i, options) {

The next thing to keep in mind is that the field is actually the jquery object wrapping the element. 接下来要记住的是,该字段实际上是包装元素的jquery对象。 So in order to look things like its value or existence, you should use field.val() 所以为了看看它的值或存在,你应该使用field.val()

if (IsNumeric(field.val())) {

helper function 辅助功能

//source: http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric
function IsNumeric(input)
{
 return (input - 0) == input && (input+'').replace(/^\s+|\s+$/g, "").length > 0;
}

This is the end result, 这是最终结果,

html HTML

<p>Enter text in the input and then focus out to see the validation at work.</p>
<b>Associate Number (EIN):</b><br>
<form>
<input type="text" id="EIN" name="EIN" class="validate[required,funcCall[einTest]]" data-prompt-position="inline" />
</form>

js JS

window.einTest = function(field, rules, i, options) {
 if (IsNumeric(field.val())) {
  var x = field.val();
  if (x.length != 5) {
   return options.allrules.ein.alertText2;
  }
 }
 else {
  return options.allrules.ein.alertText;
 }
};

function IsNumeric(input)
{
 return (input - 0) == input && (input+'').replace(/^\s+|\s+$/g, "").length > 0;
}
//plus library initialization

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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