Javascript 无法使jquery.validation在我的ASP.NET核心MVC6项目中工作

Javascript 无法使jquery.validation在我的ASP.NET核心MVC6项目中工作,javascript,jquery,Javascript,Jquery,我想我会从一个能给我实际答案的地方开始工作。它在工作,因为它在以前的职位,但我不能让它在项目中工作。我认为代码是正确的,只是在提交时没有验证文本框 这是页面的标记: <form action="/CompanyDetails/Edit" method="post"> <div class="form-horizontal"> <h4>CompanyDetailsViewModel</h4> <hr /> &l

我想我会从一个能给我实际答案的地方开始工作。它在工作,因为它在以前的职位,但我不能让它在项目中工作。我认为代码是正确的,只是在提交时没有验证文本框

这是页面的标记:

<form action="/CompanyDetails/Edit" method="post">
  <div class="form-horizontal">
    <h4>CompanyDetailsViewModel</h4>
    <hr />
    <div class="text-danger validation-summary-errors">
      <ul>
        <li style="display:none"></li>
      </ul>
    </div>
    <input type="hidden" data-val="true" data-val-required="The CompanyDetailsId field is required." id="CompanyDetailsId" name="CompanyDetailsId" value="1" />
    <div class="form-group">
      <label class="col-md-2 control-label" for="ABN">ABN:</label>
      <div class="col-md-10">
        <input type="text" name="ABN" id="ABN" class="form-control" />
        <span class="text-danger field-validation-error" data-valmsg-for="ABN" data-valmsg-replace="true" />
      </div>
    </div>

CompanyDetailsViewModel公司

荷兰银行:
“表单”按钮:

<div class="form-group">
  <div class="col-md-offset-2 col-md-10">
    <input type="submit" value="Save" class="btn btn-default" />
  </div>
</div>

表单呈现,但当我单击“保存”而不是“显示错误”时,它会返回控制器,但ModelState为false

我已经通过Firefox在实际的验证脚本上设置了断点,但它甚至没有启动


为什么验证脚本在asp.net页面中不工作,而在JSFIDLE中工作?

我在几个小时的碰壁后才解决了这个问题。事实证明,验证是根据您在视图使用的模型中设置的内容在Asp.net core中自动完成的。因此,如果您有一个类似[Requred]的dataannotation,那么它将作为taghelper转换的一部分添加到标记中

所以我真正想要的是“添加”另一条规则,而不是验证我拥有的规则。实际情况是ABN已经有了一个[必需的]规则,所以我的自定义规则没有发生任何变化,因为它需要添加。一旦我主动添加了一条新规则,一切就开始起作用了。我决定添加另一个自定义规则来检查字符串的长度,看看这些规则是否是连续的。。也就是说,一旦发现“false”,该标识符的验证将停止。在这种情况下,向澳大利亚税务局的算法验证ABN的最后一条规则也有一个长度检查,但如果位数不符,则永远不会到达那里

顺便说一句,该脚本被放置在验证脚本之后的@section Scripts{}中,因此它会在所有操作之后呈现

顺便说一句,如果我有任何错误,我很高兴被反驳,因为我还在学习

无论如何,代码如下:

    <script>
    $(function () {

        // Your custom validation method
        jQuery.validator.addMethod('abnValidate', function abnValidate(value, element) {
            if (value.length != 11 || isNaN(parseInt(value)))
                return false;
            var weighting = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
            var tally = (parseInt(value[0]) - 1) * weighting[0];
            for (var i = 1; i < value.length; i++) {
                tally += (parseInt(value[i]) * weighting[i]);
            }
            return (tally % 89) == 0;
        });

        jQuery.validator.addMethod('exactlength', function (value, element) {
            if (value.length != 11 || isNaN(parseInt(value)))
                return false;
            return true;
        });

        $("#ABN").rules("add", {
            required: true,
            exactlength: true,
            abnValidate: true,
            messages: {
                required: "Required input",
                exactlength: "ABN has to be 11 digits long.",
                abnValidate: "The ABN does not conform to the ATO's algorithm"
            } 
        });
    });
</script>

$(函数(){
//您的自定义验证方法
jQuery.validator.addMethod('abnValidate',函数abnValidate(值,元素){
if(value.length!=11 | | isNaN(parseInt(value)))
返回false;
var权重=[10,1,3,5,7,9,11,13,15,17,19];
var-tally=(parseInt(值[0])-1)*权重[0];
对于(变量i=1;i