简体   繁体   English

如何在 asp.net 核心 mvc 中停止对表单加载事件的 jquery 验证?

[英]How to stop jquery validation on form load event in asp.net core mvc?

Login.cshtml登录.cshtml

@model UserModel
<script> src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.19.0/jquery.validate.min.js">  
</script>   
<script src="https://ajax.aspnetcdn.com/ajax/mvc/5.2.3/jquery.validate.unobtrusive.min.js">  
</script>  
<form id="form-submit" asp-controller="User" asp-action="Update" method="post" 
class="form-horizontal">
 <div class="form-group padding-left">
        <label asp-for="Password">Password</label>
        <input type="password" asp-for="Password" class="form-control" />
        <span asp-validation-for="Password" class="text-danger"></span>
    </div>
</form>


public class UserModel
{
    [Required]        
    [DataType(DataType.Password)]
    public string Password { get; set; }
 }

  //controller action
  public async Task<IActionResult> Update([FromQuery] UserModel)
   {
     return View("Login", UserModel);
   }

With above markup code & jquery validation script, form field validation happens even before user interaction with input fields ie, when form loading up on the browser, it is loading up the fields along with validation related error messages.使用上述标记代码和 jquery 验证脚本,表单字段验证甚至在用户与输入字段交互之前发生,即,当表单在浏览器上加载时,它正在加载字段以及与验证相关的错误消息。

But my expectation is form field validation should happen ONLY when user interacts with the controls and provided some invalid inputs.但我的期望是表单字段验证应该只在用户与控件交互并提供一些无效输入时发生。 What Im missing?我缺少什么? How to achieve it?如何实现?

You can change the view code, Validation is only triggered when the user clicks the submit button您可以更改视图代码,仅当用户单击提交按钮时才会触发验证

@model UserModel
<form id="form-submit" asp-controller="User" asp-action="Update" method="post" 
class="form-horizontal">
 <div class="form-group padding-left">
        <label asp-for="Password">Password</label>
        <input type="password" asp-for="Password" class="form-control" />     
         <span asp-validation-for="Password" class="text-danger"></span>
    </div>
    <button type="submit">submit</button>
</form>

@section Scripts
{
   <script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
   <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
}

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.Net MVC 5 jQuery验证不适用于带有提交事件的Ajax帖子,显示为有效表单 - Asp.Net MVC 5 Jquery validation not working on ajax post with submit event, showing as valid form 如果客户端验证失败,如何在ASP.NET MVC注册表单上停止Ajax微调器 - How to stop ajax spinner on ASP.NET MVC register form if client side validation fails Javascript function 通过 ASP.NET 核心验证 Jquery 验证表单 - Javascript function to validate form by ASP.NET Core Jquery validation 使用Telerik(ASP.NET MVC)进行jQuery表单验证 - jQuery form validation using Telerik (ASP.NET MVC) 在asp.net mvc4应用程序中使用Jquery验证表单 - validation of form with Jquery within asp.net mvc4 application 使用jQuery验证的ASP.Net MVC Ajax表单 - ASP.Net MVC Ajax form with jQuery validation jQuery选项卡中的ASP.net MVC表单验证 - ASP.net mvc Form validation within Jquery Tab ASP.Net MVC 2 - jQuery验证和表单提交 - DataAnnotations - ASP.Net MVC 2 - jQuery Validation and Form Submit - DataAnnotations ASP.NET MVC:jQuery UI可选表单发布和验证 - ASP.NET MVC: jQuery UI Selectable Form Posting and Validation Asp.net Core中带有Ajax的验证表 - Validation Form With Ajax in Asp.net Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM