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 override ValidationResult IsValid(
object value, ValidationContext validationContext)
{
try
{
if (value.ToString().Length != 18)
{
return new ValidationResult("身份证号长度非18位!");
}
long n = 0;
if (long.TryParse(value.ToString().Remove(17), out n) == false
|| n < Math.Pow(10, 16) || long.TryParse(value.ToString().Replace('x', '0').Replace('X', '0'), out n) == false)
{
return new ValidationResult("身份证号长度非数字!");
}
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(value.ToString().Remove(2)) == -1)
{
return new ValidationResult("身份证号中头信息有误!(省份、区号)");
}
string birth = value.ToString().Substring(6, 8).Insert(6, "-").Insert(4, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, out time) == false)
{
return new ValidationResult("身份证号中生日信息有误!");
}
string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
char[] Ai = value.ToString().Remove(17).ToCharArray();
int sum = 0;
for (int i = 0; i < 17; i++)
{
sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
}
int y = -1;
Math.DivRem(sum, 11, out y);
if (arrVarifyCode[y] != value.ToString().Substring(17, 1).ToLower())
{
return new ValidationResult("身份证号中校验码信息有误!");
}
return ValidationResult.Success;
}
catch (Exception ex)
{
return new ValidationResult("身份证号非法,校验失败!");
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace _SCscHero.Model.01CustomAttribute
{
/// <summary>
/// List集合验证特性
/// </summary>
public class ListAttribute : ValidationAttribute
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="isNull">是否为空</param>
/// <param name="minCount">最小数量</param>
public ListAttribute(bool isNull = false, int minCount = 1)
{
IsNull = isNull;
MinCount = minCount;
}
/// <summary>
/// 是否为空
/// </summary>
public bool IsNull { get; }
/// <summary>
/// key集合
/// </summary>
public int MinCount { get; }
/// <summary>
/// 错误时说明
/// </summary>
/// <returns></returns>
public string GetErrorMessage() =>
$"当前 list集合等于null 或 小于1个集合.";
/// <summary>
/// 验证
/// </summary>
/// <param name="value"></param>
/// <param name="validationContext"></param>
/// <returns></returns>
protected override ValidationResult IsValid(object value,
ValidationContext validationContext)
{
var list = value as List<string>;
if (!IsNull && list == null)
{
return new ValidationResult("当前集合不可为空");
}
else if (list.Count < MinCount)
{
return new ValidationResult("当前集合不可小于");
}
return ValidationResult.Success;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace _SCscHero.Model.01CustomAttribute
{
/// <summary>
/// 模型验证特性-指定字符
/// </summary>
public class AgreedCharAttribute : ValidationAttribute
{
private string[] _AgreedArray;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="AgreedArray"></param>
public AgreedCharAttribute(string[] AgreedArray)
{
_AgreedArray = AgreedArray;
}
/// <summary>
/// 验证逻辑
/// </summary>
/// <param name="value">当前属性值</param>
/// <param name="validationContext">上下文</param>
/// <returns></returns>
protected override ValidationResult IsValid(
object value, ValidationContext validationContext)
{
//将value值转成string与字符串数组进行比对
if (_AgreedArray.Contains(value.ToString()))
return ValidationResult.Success;
else
return new ValidationResult(GetErrorMessage(validationContext.DisplayName));
}
/// <summary>
/// 错误提示
/// </summary>
/// <returns></returns>
public string GetErrorMessage(string element) =>
$"该传入参数{element}非法!";
}
}