繁体   English   中英

自定义 Iban 数据注释客户端验证

[英]Custom Iban data annotation clientside validation

我目前正在处理一个表单,并希望 IBAN 验证响应@client 端。 标准的 c# .net 数据注释都可以工作,但我的自定义 IBAN 数据注释有问题。不显眼的 jquery 插件是我用来向客户端获取错误消息的插件,但这并不能解决 iban 验证的问题。 iban 验证属性确实适用于提交,但我想要直接响应。

我看过一些他们实现 IClientModelValidator 的帖子,但我不知道在这种情况下如何做到这一点。

 public static class IbanValidator
{
    public static bool Validate(string iban)
    {
        if (string.IsNullOrEmpty(iban))
            return false;

        return ValidateChecksum(iban.ToUpper());
    }

    /// <summary>
    ///     Validates IBAN checksum
    /// </summary>
    /// <param name="iban">IBAN string</param>
    /// <returns>true/false</returns>
    private static bool ValidateChecksum(string iban)
    {
        if (iban.Length < 4 || iban[0] == ' ' || iban[1] == ' ' || iban[2] == ' ' || iban[3] == ' ')
            return false;

        var checksum = 0;
        var ibanLength = iban.Length;

        for (int charIndex = 0; charIndex < ibanLength; charIndex++)
        {
            if (iban[charIndex] == ' ') continue;

            int value;
            var c = iban[(charIndex + 4) % ibanLength];
            if (c >= '0' && c <= '9')
            {
                value = c - '0';
            }
            else if (c >= 'A' && c <= 'Z')
            {
                value = c - 'A';
                checksum = (checksum * 10 + value / 10 + 1) % 97;
                value %= 10;
            }
            else if (c >= 'a' && c <= 'z')
            {
                value = c - 'a';
                checksum = (checksum * 10 + value / 10 + 1) % 97;
                value %= 10;
            }
            else
            {
                return false;
            }

            checksum = (checksum * 10 + value) % 97;
        }

        return checksum == 1;
    }
}


public class IbanValidationAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        return IbanValidator.Validate(value as string)
            ? ValidationResult.Success
            : new ValidationResult(GetErrorMessage(validationContext));
    }

    private string GetErrorMessage(ValidationContext validationContext)
    {
        LocService errorTranslation = validationContext.GetService(typeof(LocService)) as LocService;
        return errorTranslation.GetLocalizedHtmlString("ErrorMessage_Invalid_Iban");
    }
}

您仍然需要创建一个单独的 Js 文件来处理客户端验证。

例子:

// Value is the element to be validated, params is the array of name/value 
$.validator.addMethod("iban", function (value, element, params) {
    // Code here
});

然后,您需要将其添加到不显眼的适配器中:

  • jQuery.validator.unobtrusive.adapters.addBool - 当您的验证器不需要任何额外数据时使用。
  • jQuery.validator.unobtrusive.adapters.addSingleVal - 当您的验证器接收一份额外数据时使用。
  • jQuery.validator.unobtrusive.adapters.addMinMax - 当您的验证器处理最小值和最大值(例如范围或字符串长度)时使用。

或者:

jquery-validation提供客户端验证作为插件:
https://github.com/jquery-validation/jquery-validation/blob/master/src/additional/iban.js

暂无
暂无

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

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