blob: 19c82edb6c9c34ced6e949a1b051a3b2a12a49cb (
plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
using System;
using System.Net.Mail;
namespace Timeline.Models.Validation
{
public abstract class OptionalStringValidator : IValidator
{
public bool Validate(object value, out string message)
{
if (value == null)
{
message = ValidationConstants.SuccessMessage;
return true;
}
if (value is string s)
{
if (s.Length == 0)
{
message = ValidationConstants.SuccessMessage;
return true;
}
return DoValidate(s, out message);
}
else
{
message = "Value is not of type string.";
return false;
}
}
protected abstract bool DoValidate(string value, out string message);
}
public static class UserDetailValidators
{
public class QQValidator : OptionalStringValidator
{
protected override bool DoValidate(string value, out string message)
{
if (value.Length < 5)
{
message = "QQ is too short.";
return false;
}
if (value.Length > 11)
{
message = "QQ is too long.";
return false;
}
foreach (var c in value)
{
if (!char.IsDigit(c))
{
message = "QQ must only contain digit.";
return false;
}
}
message = ValidationConstants.SuccessMessage;
return true;
}
}
public class EMailValidator : OptionalStringValidator
{
protected override bool DoValidate(string value, out string message)
{
if (value.Length > 50)
{
message = "E-Mail is too long.";
return false;
}
try
{
var _ = new MailAddress(value);
}
catch (FormatException)
{
message = "The format of E-Mail is bad.";
return false;
}
message = ValidationConstants.SuccessMessage;
return true;
}
}
public class PhoneNumberValidator : OptionalStringValidator
{
protected override bool DoValidate(string value, out string message)
{
if (value.Length > 14)
{
message = "Phone number is too long.";
return false;
}
foreach (var c in value)
{
if (!char.IsDigit(c))
{
message = "Phone number can only contain digit.";
return false;
}
}
message = ValidationConstants.SuccessMessage;
return true;
}
}
}
}
|