46 lines
1.2 KiB
C#
46 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace Appointment.Entity.ValidateAttribute
|
|
{
|
|
/// <summary>
|
|
/// 字符串长度校验特性
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Add:yuangb 2019-3-5
|
|
/// 使用格式:实体属性前加[StringLength(50, MinLength = 2, ErrorMessage = "获取微信用户信息失败")]
|
|
/// </remarks>
|
|
public class StringLengthAttribute : ValidateAttribute
|
|
{
|
|
/// <summary>
|
|
/// 最小长度
|
|
/// </summary>
|
|
public int MinLength { get; set; }
|
|
|
|
/// <summary>
|
|
/// 报错提示
|
|
/// </summary>
|
|
public string ErrorMessage { get; set; }
|
|
|
|
private int _maxLength;
|
|
|
|
public StringLengthAttribute(int maxLength)
|
|
{
|
|
this._maxLength = maxLength;
|
|
}
|
|
|
|
public override string Validate(object oValue)
|
|
{
|
|
if (oValue == null || string.IsNullOrWhiteSpace(oValue.ToString()) || oValue.ToString().Length > this._maxLength || oValue.ToString().Length < this.MinLength)
|
|
{
|
|
return ErrorMessage;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|