jQuery验证-form.valid()始终返回true [英] jQuery Validation - form.valid() always returning true

查看:975
本文介绍了jQuery验证-form.valid()始终返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,当我尝试检查表单是否有效时,但是form.valid()始终返回true.但是,如果我尝试验证单个控件,它将返回false.

I have a situation when I try to check if a form is valid, but form.valid() always returns true. But if I try to validate the individual control, it returns false.

这是我的表格:

<div class="profilestagsedit">
    @using (Html.BeginForm("", "", FormMethod.Post, new { id = "tagsform" }))
    {
        @Html.ValidationMessageFor(x => x.EditTagsText)
        @Html.TextBoxFor(p => p.EditTagsText, new
           {
               @id = "txtprofileedittags"
           })
    }
</div>

这是我的视图模型:

[Required(AllowEmptyStrings = false, ErrorMessage = "Please enter at least one Tag ")]
public string EditTagsText { get; set; }

这是jQuery代码:

This is the jQuery code:

   $('#tagsform').removeData("validator");
   $('#tagsform').removeData("unobtrusiveValidation");
   $.validator.unobtrusive.parse('#tagsform');
   $('#tagsform').validate();

然后在保存按钮上单击:

And on the save button click:

var isValid = $('#tagsform').validate().element($('#txtprofileedittags')); <-- false
$('#tagsform').valid() true <--

我希望form.valid()也返回false,这是我做错了吗?

I would like the form.valid() return false as well, what am I doing wrong?

推荐答案

您应该在输入中添加必填项

You should add required to the input

<input type="text" name="name" required>

请参见工作演示此处

对于不打扰的HTML 5兼容属性,描述要附加到输入字段data-val="true"的验证器.

For unobtrusive HTML 5-compatible attributes describe the validators to be attached to the input fields data-val="true".

查看更多信息此处

这篇关于jQuery验证-form.valid()始终返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆