我知道有很多类似的问题,但没有一个真正有用。
我在一页上有两种形式(某些字段是共享的),一次只能提交一种形式。我以为是因为某些字段是共享的,但是我从页面上完全删除了第二个表单,并且错误仍然存在。
我正在实现自定义验证器,但是当我调用.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>
}
这是单击“注册”时调用的jQuery:
function driverRegister() {
debugger;
var test1 = $('#driver-form')
var test = $('#driver-registration-form input');
alert(test.valid())
if (test.valid() == true) {
$('#driver-form').submit();
}
}
最佳答案
好吧,用$(document).ready(function(){})包装验证函数;
似乎解决了问题。谢谢大家的帮助:)