jquery 添加新方法的验证器不起作用

jquery validator adding a new method doesn't work

我不明白 dnicheck 的工作方法。如果我输入错误的数字,我将看不到任何消息。

我的main.js

$.validator.setDefaults({
   errorClass: 'form_error',
   errorElement: 'div'
});

$.validator.addMethod("dniCheck", function(value, element){
    var valor = false;
    if(/^([0-9]{8})*[a-zA-Z]+$/.test(value)){
        var numero = value.substr(0, value.length-1);
        numero = numero % 23;
        var let = 'TRWAGMYFPDXBNJZSQVHLCKET';
        letra = let.substring(numero,numero+1);
        if(letra===let) valor = true;
    }
    valor = this.optional(element);
    console.log(valor);
    return valor;
},"DNI no válido");

$("#form_participante").validate({
   rules:{
       nie:{
           dniCheck: true
       }
   },
   messages:{
       nie:{
           dniCheck:"Introduce el dni correcto"
       }
   }
});

我的表格

            <form id="form_participante" action="{{ urlFor('AltaParticipante') }}" method="POST" class="form-horizontal">
                <fieldset>
                    <legend>Alta nuevo participante</legend>
                    <div class="form-group">
                        <label class="col-md-4 control-label" for="nieP">Nie participante</label>
                        <div class="col-md-5">
                            <input id="nieP" name="nieP" placeholder="Nie del participante..." class="form-control input-md" type="text" />
                        </div>    
                    </div>
...
</form>

在其他形式中,我很好地验证了字段

我没有收到任何错误。

存储库在 github => https://github.com/Mangulomx/olimpiada

您脚本的第一行漏掉了 $?

您字段的 namenieP 但您在 .validate() 方法中将其拼错为 niename 属性 必须 匹配。

$("#form_participante").validate({
    rules: {
        nie: { // <-  MUST match the NAME attribute
            dniCheck: true
        }
    }, .....

演示:http://jsfiddle.net/vukqgv8b/