简体   繁体   English

如何在ASP.NET MVC的客户端中触发自定义验证器

[英]How to fire a custom validator in client side at ASP.NET MVC

This is my custom validator class: 这是我的自定义验证器类:

public class PriceAttribute : ValidationAttribute {

    public double MinPrice { get; set; }

    public override bool IsValid(object value) {
        if (value == null) {
            return true;
        }
        var price = (double)value;
        if (price < MinPrice) {
            return false;
        }
        double cents = price - Math.Truncate(price);
        if (cents < 0.99 || cents >= 0.995) {
            return false;
        }

        return true;
    }
}

And my Model: 而我的模特:

public class MyModel {

    public long Id { get; set; }

    [Price(MinPrice = 1.2, ErrorMessage = "hmm not good value")]
    public double Price { get; set; }
}

But this validator fires with postback. 但是,此验证程序将触发回发。 How can I implement it to fire client side like the [Required] validator. 我如何像[Required]验证器一样实现它来激发客户端。 Is there any jQuery reference to add in the view page? 在视图页面中是否要添加任何jQuery参考? Or do I need a custom script to handle it? 还是我需要一个自定义脚本来处理它?

This article describes in detail, which steps you have to do in order to also get client side validation. 本文详细介绍了您还必须执行哪些步骤才能获得客户端验证。

but this validator fire with postback 但是这个验证器会发回邮件

Please note there is no postback, just plain HTTP POSTs in MVC. 请注意,在MVC中没有回发,只有纯HTTP POST。

您最好使用jQuery验证引擎。

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

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