简体   繁体   English

无法在我的ASP.NET Core MVC6项目中使用jquery.validation

[英]Unable to get jquery.validation working in my ASP.NET Core MVC6 project

I thought I would have this working from a post that gave me the actual answer. 我以为我会从提供实际答案的帖子中进行这项工作。 it works in JSfiddle as it did in the previous post however I cannot get it to work in the project. 它在JSfiddle中可以像在上一篇文章中那样工作,但是我无法在项目中使用它。 I think the code is right it's just not validating the textbox on submit. 我认为代码是正确的,只是不验证提交时的文本框。

This is the markup for page: 这是页面的标记:

<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>

The form button: 表单按钮:

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

The form renders but when I click on save instead of it showing an error it just goes back to the controller however the ModelState is false. 表单呈现,但是当我单击“保存”而不是显示错误时,它将返回到控制器,但是ModelState为false。

I have set breakpoints on the actual validate script via Firefox but it doesn't even fire. 我已经通过Firefox在实际的验证脚本上设置了断点,但是它甚至没有触发。

Why is the validate script not working in the asp.net page while its working in JSFiddle ? 为什么验证脚本在JSFiddle中运行时在asp.net页中不起作用

I worked this out after hours of banging my head against the wall. 在将我的头撞到墙上几个小时后,我才解决了这个问题。 Turns out that validation is done in Asp.net core automatically based on the what you set up in the model being used by the view. 事实证明,根据您在视图所使用的模型中设置的内容,将自动在Asp.net core中完成验证。 So if you have a dataannotation like [Requred] then this will be added into the markup as part of the taghelper conversion. 因此,如果您有一个像[Requred]这样的数据注释,那么它将作为taghelper转换的一部分添加到标记中。

So what I really wanted was to "add" another rule not validate it which I had. 因此,我真正想要的是“添加”另一条规则而不验证我所拥有的规则。 what actually happened was that the ABN already had a [Required] rule so nothing happened for my custom rule as it needed to be added. 实际发生的是,ABN已经有一个[Required]规则,因此我的自定义规则没有任何反应,因为需要添加它。 Once I actively added a new rule then it all started to work. 一旦我积极添加了新规则,那么所有规则都将开始起作用。 I decided to add in another custom rule checking the length of the string to see if the rules were sequential.. that is, validation for that identifier would stop once a "false" was found. 我决定添加另一个自定义规则,以检查字符串的长度,以查看规则是否是顺序的。也就是说,一旦找到“ false”,该标识符的验证就会停止。 In this case the last rule that validates the ABN to the Australian Taxation Office's algorithm also has a check for length but never gets there if the number of digits doesnt tally. 在这种情况下,根据澳大利亚税务局算法对ABN进行验证的最后一条规则也对长度进行了检查,但是如果数字的位数不一致,则永远不会到达该长度。

Incidentally, the script has been placed in the @section Scripts{ } after the validation scripts so it renders after everything... 顺便说一句,该脚本已放置在验证脚本之后的@section Scripts {}中,因此在所有内容之后都会呈现。

By the way happy to be contradicted if I have any of this wrong as I am still just learning.. 顺便说一句,如果我仍然在学习中遇到任何这样的错误,很乐意自相矛盾。

Anyway here is the code: 无论如何,这里是代码:

    <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>

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

相关问题 Asp.net MVC中的jquery验证错误 - 无法获取属性&#39;call&#39;的值:object为null或undefined - jquery validation error in Asp.net MVC - unable to get value of property 'call' : object is null or undefined JQuery 缓动在 asp.net mvc 项目中不起作用 - JQuery Easing Not Working in asp.net mvc project 无法使Dojo定制模块与ASP.net MVC一起使用 - Unable to get Dojo custom module working with ASP.net MVC ASP.NET Core项目中的jQuery和Bootstrap - jQuery & Bootstrap in ASP.NET Core project JQuery验证不适用于asp.net文本框 - JQuery validation Not working for asp.net textbox ASP.NET MVC3:为什么我的自定义验证属性永远不起作用? - ASP.NET MVC3: Why are my custom validation attributes never working? Asp.Net MVC 5 jQuery验证不适用于带有提交事件的Ajax帖子,显示为有效表单 - Asp.Net MVC 5 Jquery validation not working on ajax post with submit event, showing as valid form 数据注释在部分ASP.NET MVC中无法使用jquery进行非侵入式验证 - Data Annotations not working jquery unobtrusive validation in Partial ASP.NET MVC 添加jQuery click事件后,使用asp.net mvc进行的客户端验证停止工作 - Client side validation with asp.net mvc stopped working after I added a jQuery click event Javascript function 通过 ASP.NET 核心验证 Jquery 验证表单 - Javascript function to validate form by ASP.NET Core Jquery validation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM