简体   繁体   English

使用jqueries验证插件,如何从服务器端向客户端发送错误?

[英]Using jqueries validation plugin, how to send an error from the server side to the client side?

I have a custom validation check that I perform on the server sidie (.net mvc). 我在服务器sidie(.net mvc)上执行了自定义验证检查。

If it fails, I want to notify the user with a message next to the input box (just how jqueries validation plugin works out of box). 如果失败,我想在输入框旁边用一条消息通知用户(即jqueries验证插件的开箱即用)。

Is there a built in function (in the validation plugin) I can call to do this? 是否可以调用一个内置函数(在验证插件中)?

Update The solution would ideally be using the plugin itself, maybe there is a method call I can use? 更新解决方案理想情况下将使用插件本身,也许有一个我可以使用的方法调用?

Yes, there is an example of this. 是的,有一个例子

You basically rules: {FIELD: {remote: "URL"}} , then on validation a call is made to URL?FIELD=value and on the server you return true or false . 您的基本rules: {FIELD: {remote: "URL"}} ,然后在验证时调用URL?FIELD=value然后在服务器上返回truefalse

There is an open source project called xVal that does this. 有一个名为xVal的开源项目可以做到这一点。 Here's an excerpt from the project home page: 这是项目主页的摘录:

Project Description xVal is a validation framework for ASP.NET MVC applications. 项目描述xVal是ASP.NET MVC应用程序的验证框架。 It makes it easy to link up your choice of server-side validation mechanism with your choice of client-side validation library, neatly fitting both into ASP.NET MVC architecture and conventions. 它使将您选择的服务器端验证机制与您选择的客户端验证库链接起来变得容易,从而使它们既适合ASP.NET MVC体系结构又适合于约定。 Features 特征

  • Define your validation rules using attributes on model properties, eg, 使用模型属性上的属性定义验证规则,例如

    [Required] [StringLength(50)] public string Name { get; [必需] [StringLength(50)]公共字符串名称{ set; 组; } }

(Or, if you prefer, you can supply rules programatically or you can just hard-code them in specific views) (或者,如果您愿意,可以以编程方式提供规则,也可以仅在特定视图中对其进行硬编码)

  • Designed to fit into ASP.NET MVC conventions for handling form posts and storing and retrieving error information in ModelState 设计成适合于ASP.NET MVC约定,用于处理表单发布以及在ModelState中存储和检索错误信息
  • Use your choice of server-side validation framework. 使用您选择的服务器端验证框架。 Out of the box, xVal lets you use .NET 3.5's built-in DataAnnotations classes or Castle Validator (or both). 使用xVal即可立即使用.NET 3.5的内置DataAnnotations类或Castle Validator(或同时使用两者)。 If you want to use something different, you can create your own provider just by implementing IRulesProvider. 如果要使用其他功能,则可以仅通过实现IRulesProvider来创建自己的提供程序。
  • Use your choice of client-side validation library. 使用您选择的客户端验证库。 Out of the box, xVal lets you use jQuery Validation or ASP.NET's native client-side validation library (aka WebUIValidation.js, as used by WebForms). 使用xVal可以立即使用jQuery Validation或ASP.NET的本机客户端验证库(也就是WebForms使用的WebUIValidation.js)。 Or, use any other client-side validation library by writing a plug-in to configure it using xVal's standard JSON rules description format. 或者,通过编写插件使用xVal的标准JSON规则描述格式对其进行配置,从而使用任何其他客户端验证库。
  • Supports localized error messages using resource files. 支持使用资源文件的本地化错误消息。 Vary the language of your validation messages according to the current thread's culture. 根据当前线程的文化来更改验证消息的语言。
  • Add custom validation logic that runs both on the server and on the client, either by subclassing an existing rule or by referencing a custom JavaScript function 通过子类化现有规则或引用自定义JavaScript函数,添加可在服务器和客户端上运行的自定义验证逻辑

You could create a custom validation function and couple it with an ajax call. 您可以创建一个自定义验证功能,并将其与ajax调用耦合。 In this case the ajax call will return the text 'true' or 'false'. 在这种情况下,ajax调用将返回文本“ true”或“ false”。

var customFunction = function(value, element, param) {
    var isValid = false;
    $.ajax({
        url: '/something',
        data:{value: value},
        success: function(data, textStatus) {isValid = data;},
        dataType: 'json',
        async: false
    });
    return isValid;
};

$.validator.addMethod("customFunction", customFunction, "That isn't valid.");

$('form').validate({
    rules: {
        fieldname: {
            customFunction: true
        }
    }
});

Edit : Actually, use the remote method that svinto mentions instead. 编辑 :实际上,实际上使用svinto提到的远程方法。 I didn't even know that existed. 我什至不知道那存在。 I'm a fool. 我是一个傻瓜。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM