aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Models/Validation
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-22 15:29:03 +0800
committerGitHub <noreply@github.com>2019-08-22 15:29:03 +0800
commit11f01c56b4ea1dbb09d04258bec89f800c6ee2b6 (patch)
treeaf83f8596b4fa78713733c0db6b4b6d1695d0ff0 /Timeline/Models/Validation
parentfd95f9abc017575b13a31dd16ac72ef663e984d6 (diff)
parent96c18fb2e17c94ff04094608c705db087400f510 (diff)
downloadtimeline-11f01c56b4ea1dbb09d04258bec89f800c6ee2b6.tar.gz
timeline-11f01c56b4ea1dbb09d04258bec89f800c6ee2b6.tar.bz2
timeline-11f01c56b4ea1dbb09d04258bec89f800c6ee2b6.zip
Merge pull request #48 from crupest/user-details
Add user details.
Diffstat (limited to 'Timeline/Models/Validation')
-rw-r--r--Timeline/Models/Validation/UserDetailValidator.cs116
1 files changed, 116 insertions, 0 deletions
diff --git a/Timeline/Models/Validation/UserDetailValidator.cs b/Timeline/Models/Validation/UserDetailValidator.cs
new file mode 100644
index 00000000..19c82edb
--- /dev/null
+++ b/Timeline/Models/Validation/UserDetailValidator.cs
@@ -0,0 +1,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;
+ }
+ }
+ }
+}