简体   繁体   English

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

[英]Custom Iban data annotation clientside validation

I'm currently working on a form and would like to have the IBAN validation responsive @client side.我目前正在处理一个表单,并希望 IBAN 验证响应@client 端。 The standard c# .net data annotations are all working but i have a problem with my custom IBAN data annotation.The unobtrusive jquery plugin is what i'm using to get the errormessages to the client but thats not doing the trick for the iban validation.标准的 c# .net 数据注释都可以工作,但我的自定义 IBAN 数据注释有问题。不显眼的 jquery 插件是我用来向客户端获取错误消息的插件,但这并不能解决 iban 验证的问题。 The iban validation attribute does work on submit but i would like to have a direct reponsive. iban 验证属性确实适用于提交,但我想要直接响应。

I have seen some posts where they implement the IClientModelValidator but i dont have a clue how to do this in this situation.我看过一些他们实现 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");
    }
}

You would still need to create a separate Js file that handles the client side validation.您仍然需要创建一个单独的 Js 文件来处理客户端验证。

Example:例子:

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

You'll then need to add it to the unobtrusive adapters:然后,您需要将其添加到不显眼的适配器中:

  • jQuery.validator.unobtrusive.adapters.addBool - used when your validator does not need any additional data. jQuery.validator.unobtrusive.adapters.addBool - 当您的验证器不需要任何额外数据时使用。
  • jQuery.validator.unobtrusive.adapters.addSingleVal - used when your validator takes in one piece of additional data. jQuery.validator.unobtrusive.adapters.addSingleVal - 当您的验证器接收一份额外数据时使用。
  • jQuery.validator.unobtrusive.adapters.addMinMax - used when your validator deals with minimum and maximum values such as range or string length. jQuery.validator.unobtrusive.adapters.addMinMax - 当您的验证器处理最小值和最大值(例如范围或字符串长度)时使用。

OR:或者:

jquery-validation offers the Client side validation as plugin: jquery-validation提供客户端验证作为插件:
https://github.com/jquery-validation/jquery-validation/blob/master/src/additional/iban.js 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