为什么.valid()总是返回TRUE?

时间:2016-12-08 15:12:49

标签: javascript jquery html validation razor

我知道有像这样的载荷和载荷问题,但它们都没有真正帮助。

我在一个页面上有两个表单(某些字段是共享的),一次只能提交一个表单。我认为这是因为某些字段是共享的,但我从页面中完全删除了第二个表单,并且错误仍然存​​在。

我正在实现自定义验证器,但是当我调用.valid()时,它总是返回TRUE,即使是规则也是如此。

目前我只有第一种形式的验证器(我没有继续前进,因为它失败了。)

$('#driver-form').validate({
rules:{
    FirstName: {
        required:true,
        minlength: 2,
    },
    LastName: {
        required:true,
        minlength: 2,
    },
    Email: {
        required: true,
        email: true
    },
    PhoneNum: {
        required: true,
        exactlength: 11,
    },        
    Password: {
        required: true,
        minlength: 6,
        symbol: true
    }
},messages:{
    Firstname: {
        required: "Please enter your firstname",
        minlength: "Firstname should contain more that 2 characters"
    },
    LastName: {
        required: "Please enter your lastname",
        minlength: "Lastname should contain more that 2 characters"
    },
}});

以下是表格:

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form", @id = "driver-form" }))
{
    @Html.HiddenFor(m => m.IsGarage, new { @class = "_HdnIsGarage" })

    <div class="registration-form" id="driver-registration-form">
        @Html.AntiForgeryToken()
        <hr />
        @Html.ValidationSummary("", new { @class = "text-danger" })
        <div>
            @Html.Kendo().TextBoxFor(m => m.FirstName).HtmlAttributes(new { placeholder = "First Name" })
        </div>
        <div>
            @Html.Kendo().TextBoxFor(m => m.LastName).HtmlAttributes(new { placeholder = "Last Name" })
        </div>
        <div>
            @Html.Kendo().TextBoxFor(m => m.Email).HtmlAttributes(new { placeholder = "E-mail", @type = "e-mail" })
        </div>
        <div>
            @Html.Kendo().TextBoxFor(m => m.PhoneNum).HtmlAttributes(new { placeholder = "Phone Number" })
        </div>
        <div>
            @Html.Kendo().TextBoxFor(m => m.Password).HtmlAttributes(new { placeholder = "Password", @type = "password" })
        </div>
        <div>
            @*@Html.Kendo().TextBoxFor(m => m.ConfirmPassword).Name("pw").HtmlAttributes(new { placeholder = "Password" }))*@
        </div>
        <div>
            @Html.Kendo().Button().Name("Register").Content("Register").HtmlAttributes(new { type = "button", onclick = "Javascript:driverRegister()" })
        </div>
    </div>
}

此处点击“注册&#39;

”时会调用jQuery
        function driverRegister() {
        debugger;
        var test1 = $('#driver-form')
        var test = $('#driver-registration-form input');
        alert(test.valid())
        if (test.valid() == true) {
            $('#driver-form').submit();
        }
    }

1 个答案:

答案 0 :(得分:0)

好吧,用$(document).ready(function(){})包装验证函数; 似乎解决了这个问题。谢谢所有帮助过的人:)。