javascript MVC 5 - 在客户端验证特定字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30946123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:59:10  来源:igfitidea点击:

MVC 5 - Validate a specific field on client-side

javascriptjqueryasp.net-mvcvalidationasp.net-mvc-5

提问by xr280xr

I want to populate a city/state drop down list based on the postal code a user types into a textbox. So when the text changes, I'm going to make an ajax call to retrieve the data. However, I only want to perform that ajax request for valid postal codes. The field already validates using the DataAnnotations.RegularExpression attribute and jquery.validate.unobtrusive validation library. I'm unclear on what can and can't be used from jquery.validate when using unobtrusive. I've looked at the unobtrusive code, but haven't gotten an understanding of it yet. So two questions:

我想根据用户在文本框中键入的邮政编码填充城市/州下拉列表。因此,当文本更改时,我将进行 ajax 调用以检索数据。但是,我只想执行对有效邮政编码的 ajax 请求。该字段已经使用 DataAnnotations.RegularExpression 属性和 jquery.validate.unobtrusive 验证库进行了验证。我不清楚在使用 unobtrusive 时可以和不能从 jquery.validate 使用什么。我看过不引人注目的代码,但还没有理解它。所以两个问题:

Using javascript,

使用javascript,

  1. is there a way to force validation on a specific field, not the whole form?
  2. is there a way to check whether a specific field is valid?
  1. 有没有办法强制验证特定字段而不是整个表单?
  2. 有没有办法检查特定字段是否有效?

回答by xr280xr

After digging around in the source code, I've come to these conclusions. First, the purpose of unobtrusive is to wire up the rules and messages, defined as data- attributes on the form elements by MVC, to jQuery.validation. It's for configuring/wiring up validation, not a complete wrapper around it, so when it comes to performing validation that is already set up, you don't have to worry about "circumventing", or not involving, unobtrusive.

在深入研究源代码后,我得出了这些结论。首先,unobtrusive 的目的是将 MVC 定义为表单元素上的数据属性的规则和消息连接到 jQuery.validation。它用于配置/连接验证,而不是围绕它的完整包装器,因此在执行已经设置的验证时,您不必担心“规避”或不涉及不显眼的问题。

So to answer the questions:

所以要回答这些问题:

  1. Yes, there are two ways. The Validator.element(element)function and the $.fn.valid()extension method. .validactually calls Validator.elementinternally. The difference is .validworks on a jQuery which allows you to perform the validation on one or more fields (or the form itself). Validator.element performs validation on only a single element and requires you to have an instance of the validator object. Although the documentation states .validate() "validates the selected form", it actually appears to initialize validation for the form, and if it has already been called, it simply returns the validator for the form. So here are examples of two ways to validate an input (below #2).

  2. Yes, but not without also performing the validation. Both of the methods from #1 return a boolean so you can use them to determine whether a field is valid. Unfortunately there doesn't appear to be anything exposed by the library that allows you to check the validation without, in effect, showing or hiding the validation message. You would have to get at and run the rule(s) for the field from your code, which may be possible, but my need didn't justify spending the time on it.

  1. 是的,有两种方法。的Validator.element(element)功能和$.fn.valid()扩展方法。.valid实际上在Validator.element内部调用。不同之处在于.validjQuery 允许您对一个或多个字段(或表单本身)执行验证。Validator.element 仅对单个元素执行验证,并要求您拥有验证器对象的实例。尽管文档声明 .validate() “验证选定的表单”,但它实际上似乎是为表单初始化验证,如果它已经被调用,它只会返回表单的验证器。因此,这里是验证输入的两种方法的示例(在 #2 下方)。

  2. 是的,但也不能不执行验证。#1 中的两种方法都返回一个布尔值,因此您可以使用它们来确定字段是否有效。不幸的是,该库似乎没有公开任何内容,允许您在实际上不显示或隐藏验证消息的情况下检查验证。您必须从代码中获取并运行该字段的规则,这可能是可能的,但我的需要并不能证明在上面花费时间是合理的。

Example:

例子:

<form>
    <input id="txtDemo" type="text"></input>
</form>
...    
<script type="text/javascript">
    $("#txtDemo").valid();

    //or

    //Get the form however it makes sense (probably not like this)
    var validator = $("form").validate();

    //Note: while .element can accept a selector, 
    //it will only work on the first item matching the selector.
    validator.element("#txtDemo");
</script>

回答by Feder Catalin

you can find if a single field is valid and trigger this validation this way: $("#myform").validate().element("#elem1");

您可以找到单个字段是否有效并通过以下方式触发此验证: $("#myform").validate().element("#elem1");

details here http://docs.jquery.com/Plugins/Validation/Validator/element#element

详情请见http://docs.jquery.com/Plugins/Validation/Validator/element#element

回答by Manohar Thakur

Use like this:

$('#Create').on('click', function () {
  var form = $('#test').closest('form');

  $(form).validate();

  if (!$(form).valid()) {
    return
  } else {
    // Bide the data 
  }
});

Hope it works for you

希望对你有帮助