using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace _SCscHero.Model.01CustomAttribute
{
/// <summary>
/// 指定长度的字符串
/// </summary>
public class AgreedLengthAttribute : ValidationAttribute
{
private int _length;
/// <summary>
/// 长度
/// </summary>
/// <param name="length"></param>
public AgreedLengthAttribute(int length)
{
_length = length;
}
/// <summary>
/// 验证
/// </summary>
/// <param name="value">当前属性值</param>
/// <param name="validationContext">上下文</param>
/// <returns></returns>
protected override ValidationResult IsValid(
object value, ValidationContext validationContext)
{
if (value.ToString().Length == _length)
return ValidationResult.Success;
else
return new ValidationResult(GetErrorMessage(validationContext.DisplayName));
}
/// <summary>
/// 错误提示
/// </summary>
/// <returns></returns>
public string GetErrorMessage(string element) =>
$"该传入参数{element}非法!";
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace _SCscHero.Model.01CustomAttribute
{
/// <summary>
/// 身份证号验证
/// </summary>
public class IdentityCardAttribute : ValidationAttribute
{
/// <summary>
/// 验证
/// </summary>
/// <param name="value">当前属性值</param>
/// <param name="validationContext">上下文</param>
/// <returns></returns>
protected