簡體   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