如果输入不是数字,则验证失败

时间:2017-12-31 13:44:14

标签: c# jquery asp.net-mvc razor data-annotations

我有一个MVC 5.2.3项目。我正在使用数据注释,包括仅在服务器上验证的远程数据验证注释。远程验证使用存储在服务器上的数字检查输入,并检查输入与输入的其他数字 另外,我在客户端有一个字段,显示输入的所有数字的总和 只要没有输入非数字字符,这样就可以正常工作。如果我进入" 5,000"然后不执行验证。我在我的javascript中添加了一行,从文本框中删除逗号。该脚本在独立的html页面中工作正常,但不适用于我的mvc页面。 js $ validator.setDefaults({onkeyup:false});是远程验证正常工作所必需的,因此不能选择删除它。此外,我尝试添加一个regedit验证注释,但它没有被触发(可能是因为我关闭了onkeyup事件)。

如果输入的值是数字而不是a.value的值是输入的值,则在函数的开头设置断点。如果输入的值有逗号或其他非数字字符,那么a.value为空 为什么会这样?

这是我的javascript:

 $.validator.setDefaults({ onkeyup: false });
$('.sumup').blur(function (a) { addvalue(this); });         
    });

    function addvalue(a) {
        var sum = 0;
        var dsum = 0;
        var b = a.value.replace(/\D/g, '');
        a.value = b;
        $('.sumup').
           each(function () {
               if ($.isNumeric($(this).val()))
               { sum += parseInt($(this).val()); }
           });
        $('#total').html(sum.toCurrencyString());

        $('#totaldirect').html(dsum.toCurrencyString());

    }
    Number.prototype.toCurrencyString = function () {
        return this.toFixed(0).replace(/(\d)(?=(\d{3})+\b)/g, '$1,');
    }

这是我的剃刀脚本:

 <th>@Html.LabelFor(model => model.CurrentFR.Salaries, htmlAttributes: new {@class = "control-label col-md-2"})</th>
            <td>@Html.EditorFor(model => model.CurrentFR.Salaries, new {htmlAttributes = new {@class = "form-control col-md-2 sumup", id = "saltb"}})
           @Html.ValidationMessageFor(model => model.CurrentFR.Salaries, "", new { @class = "text-danger" })</td>

             <td>@Html.DisplayFor(model => model.ReportedToDate.Salaries, new {htmlAttributes=new {id="salariesTD"}})</td>
            <td>@Html.DisplayFor(model => model.BudgetToDate.Salaries, new {htmlAttributes = new {id="salariesBTD"}})</td>
        </tr>
        <tr>
            <th>@Html.LabelFor(model => model.CurrentFR.Operations, htmlAttributes: new {@class = "control-label col-md-2"})

            </th>
            <td>@Html.EditorFor(model => model.CurrentFR.Operations, new {htmlAttributes = new {@class = "form-control sumup", id = "opertb"}})
                @Html.ValidationMessageFor(model => model.CurrentFR.Operations, "", new {@class = "text-danger"})</td>
            <td>@Html.DisplayFor(model => model.ReportedToDate.Operations)</td>
            <td>@Html.DisplayFor(model => model.BudgetToDate.Operations)</td>
        </tr>  ....

这是我对这些字段的数据模型:

   [DisplayFormat(DataFormatString = "${0:N0}", ApplyFormatInEditMode = true)]
    //[RegularExpression("([0-9]+)", ErrorMessage = "Please enter valid Number, No comma's or periods")]
    [Remote("ValidateSalary", "FiscalReports", HttpMethod = "POST", AdditionalFields = "Counter, FiscalYear",
        ErrorMessage = "The budget to date cannot be exceeded by more than $5,000")]
    public int? Salaries { get; set; }

    [DisplayFormat(DataFormatString = "${0:N0}"]
    [Remote("ValidateOperations", "FiscalReports", HttpMethod = "POST", AdditionalFields = "Counter, FiscalYear",
        ErrorMessage = "The budget to date cannot be exceeded by more than $5,000")]
    public int? Operations { get; set; }

0 个答案:

没有答案