0

我的问题是第一次加载页面时,弹出警报(1)和警报(2),但是当我点击我的保存按钮时警报(3)不弹出但显示验证消息(不计如果我填写正确或错误的下拉列表)并且所有内容都已保存。我有这种JS:

        <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script type="text/javascript">

        $(document).ready(function () {
            alert(1);
            PassWayValidators();
            $('#documents_TravelDocumentDocumentPass_PassDate').datepicker();           

        });

        function PassWayValidators() {
            alert(2);
            $.validator.addMethod('PassWayValidator',
                    function (value, element) {
                        alert(3);
                    }, 'Please Inert Valid Numbers');

            $("#documents_TravelDocumentDocumentPass_PassWayId").rules("add", {
                PassWayValidator: true
            });
        }
    </script>
    <...Some Code Here...>
         <div class="TravelDocumentDocumentPassCommentInner">
                                <span>გადაცემის ფორმა
                                    <br />
                                    @Html.DropDownListFor(model => documents.TravelDocumentDocumentPass.PassWayId, (IEnumerable<SelectListItem>)ViewBag.DocumentPassTypeList, String.Empty, isDisabled)
                                    @Html.ValidationMessageFor(model => documents.TravelDocumentDocumentPass.PassWayId, "*")
                                </span>
                            </div>
    <...Some Code Here too... and>
 @Html.ValidationSummary()

谁能告诉我有什么问题?

4

1 回答 1

0

由于您正在编写自定义验证器,因此我立即看到两件事需要更正。

首先,您需要PassWayValidators();在 $(document).ready() 之外调用。我不是 100% 确定它是必要的,但我认为这是我能够让自定义验证工作的唯一方法。

PassWayValidators();

$(document).ready(function () {
    $('#documents_TravelDocumentDocumentPass_PassDate').datepicker();                     
});

然后,您需要配置您的下拉菜单,以确保它通过指定某些 html 属性调用您的验证函数。

@Html.DropDownListFor(model => 
    documents.TravelDocumentDocumentPass.PassWayId, 
    (IEnumerable<SelectListItem>)ViewBag.DocumentPassTypeList, 
    String.Empty, 
    new { 
        data_val = "true",
        data_val_PassWayValidator = "Please insert valid numbers"
    })

类似的东西。我不知道 rules("add" { }) 的用途,但我不确定您是否需要。万一我错过了什么,这里是从我的一个应用程序中获取的完整示例。

<!-- some form and other html and stuff -->
@Html.TextBox("Amount", null, new 
{ 
    data_val = "true", 
    data_val_required = "This field is required", 
    data_val_number = "The field must be a number",
    data_val_notBeZero = "Value must not be zero"
})

<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>

<script type="text/javascript">
    // validators
    $.validator.addMethod("notBeZero", function (value, element)
    {
        if (Number(Math.abs(value).toFixed(2)) == 0)
        {
            return false;
        }

        return true;

    }, "Value must not be zero");

    $.validator.unobtrusive.adapters.addBool("notBeZero");
</script>
于 2012-07-26T16:07:00.417 回答