[英]Client Side Validation not working in Asp.Net Core
I am trying to use client side validation in my application but its not working.我正在尝试在我的应用程序中使用客户端验证,但它不起作用。 Only server side validation is working.
只有服务器端验证有效。 The validation works after the form is posted to the server.
表单发布到服务器后,验证工作。 I have the following scripts in my head section of the layout
我在布局的头部部分有以下脚本
<script src="~/js/jquery-3.6.0.min" type="text/javascript"></script>
<script src="~/js/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<script src="~/js/jquery.validate.js" type="text/javascript"></script>
I have used browser developer tools and the scripts are shown in the head section of the page.我使用了浏览器开发工具,脚本显示在页面的头部部分。 when I click the submit button in the form the form is being submitted and only afterwards the errors show.
当我单击表单中的提交按钮时,表单正在提交,然后才会显示错误。
The form is as follows:表格如下:
<form asp-action="Create">
<div class="form-group">
<label asp-for="@Model.Title"></label>
<textarea asp-for="@Model.Title" rows="3" class="form-control"></textarea>
<span asp-validation-for="@Model.Title" class="text-danger"></span>
</div>
<div class="text-center">
<button class="btn btn-primary form-btn-margin-top" type="submit">Create</button>
</div>
</form>
The model class is模型类是
public class Material
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
}
Update to place the .js
on the end of the jQuery path.更新以将
.js
放在 jQuery 路径的末尾。 Also remove the attribute for type="text/javascript"
all modern browsers have that as a default and it will generate a warning on validation for example here: https://validator.w3.org/还要删除
type="text/javascript"
的属性,所有现代浏览器都将其作为默认值,它会在验证时生成警告,例如: https : //validator.w3.org/
<script src="~/js/jquery-3.6.0.min.js"></script> <script src="~/js/jquery.validate.unobtrusive.min.js"></script> <script src="~/js/jquery.validate.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.