-
Notifications
You must be signed in to change notification settings - Fork 86
/
IntegerAttribute.cs
34 lines (28 loc) · 891 Bytes
/
IntegerAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.ComponentModel.DataAnnotations;
using DataAnnotationsExtensions.Resources;
namespace DataAnnotationsExtensions
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class IntegerAttribute : DataTypeAttribute
{
public IntegerAttribute()
: base("integer")
{
}
public override string FormatErrorMessage(string name)
{
if (ErrorMessage == null && ErrorMessageResourceName == null)
{
ErrorMessage = ValidatorResources.IntegerAttribute_Invalid;
}
return base.FormatErrorMessage(name);
}
public override bool IsValid(object value)
{
if (value == null) return true;
int retNum;
return int.TryParse(Convert.ToString(value), out retNum);
}
}
}