schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Controls;
 
namespace XHandler.View.Classes
{
    public class RegisterValidationRule : ValidationRule
    {
        private ValidationOutput validationOutput = null;
        public ValidationOutput ValidationOutput
        {
            get { return validationOutput; }
            set { validationOutput = value; }
        }
        private string validateType;
 
        public string ValidateType
        {
            get { return validateType; }
            set { validateType = value; }
        }
 
        public bool IsLegalPhoneNumber(string phoneNumber)
        {
            string phone = phoneNumber.Trim();
            if (phone.Length == 11)
                return Regex.IsMatch(phone, @"^0?(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[89])[0-9]{8}$");
            else
                return false; 
        }
 
        public bool IsLegalEmail(string email)
        {
            bool isValid = Regex.IsMatch(email, @"^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$");
            return isValid;
        }
 
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (ValidateType == "UserName")
            {
                string s = (string)value;
                if (string.IsNullOrEmpty(s.Trim()))
                {
                    ValidationOutput.IsNickNameValid = false;
                    ValidationOutput.IsAllValid = false;
                    ValidationOutput.ErrorMsg4NickName = Properties.UserResource.strUserNameCannotEmpty;
                    return new ValidationResult(false, Properties.UserResource.strUserNameCannotEmpty);
                }
                else
                {
                    ValidationOutput.IsNickNameValid = true;
                    ValidationOutput.IsAllValid = true;
                    return new ValidationResult(true, null);
                }
 
            }
            else if (ValidateType == "PhoneNumber")
            {
                string s = (string)value;
                if (!string.IsNullOrEmpty(s.Trim()))
                {
                    if (s.Length < 11)
                    {
                        ValidationOutput.IsPhoneNumberValid = false;
                        ValidationOutput.IsAllValid = false;
                        ValidationOutput.ErrorMsg4PhoneNumber = "电话号码长度有误";
                        return new ValidationResult(false, "电话号码长度有误");
                    }
                    if (!IsLegalPhoneNumber(s))
                    {
                        //ValidationOutput.IsShow4PhoneNumber = true;
                        ValidationOutput.IsPhoneNumberValid = false;
                        ValidationOutput.IsAllValid = false;
                        ValidationOutput.ErrorMsg4PhoneNumber = "电话号码格式不正确";
                        return new ValidationResult(false, "电话号码格式不正确");
                    }
                }
                else
                {
                    ValidationOutput.IsShow4PhoneNumber = false;
                    ValidationOutput.IsPhoneNumberValid = true;
                    ValidationOutput.IsAllValid = true;
                    return new ValidationResult(true, null);
                }
            }
            else if (ValidateType == "Password")
            {
                string myPassword = (string)value;
                ValidationOutput.Password = myPassword;//Store the password in a global resource, used for validating the confirm password
                if (myPassword.Length < 6)
                {
                    ValidationOutput.IsPasswordValid = false;
                    ValidationOutput.IsAllValid = false;
                    ValidationOutput.ErrorMsg4Password = "密码长度应大于6个字符";
                    return new ValidationResult(false, "密码长度应大于6个字符");
                }
                else
                {
                    ValidationOutput.IsPasswordValid = true;
                    ValidationOutput.IsAllValid = true;
                    return new ValidationResult(true, null);
                }
            }
            else if (ValidateType == "ConfirmPassword")
            {
                string myConfirmPassword = (string)value;
                string myPassword = ValidationOutput.Password;
                if (myPassword != myConfirmPassword)
                {
                    ValidationOutput.IsConfirmPasswordValid = false;
                    ValidationOutput.IsAllValid = false;
                    ValidationOutput.ErrorMsg4ConfirmPassword = "密码不一样";
                    return new ValidationResult(false, "密码不一样");
                }
                else
                {
                    ValidationOutput.IsConfirmPasswordValid = true;
                    ValidationOutput.IsAllValid = true;
                    return new ValidationResult(true, null);
                }
            }
            else if(ValidateType == "Email")
            {
                string email = (string)value;
                if (!string.IsNullOrEmpty(email.Trim()))
                {
                     // 检查输入的字符串是否符合Email格式
                    if(!IsLegalEmail(email.Trim()))
                    {
                        ValidationOutput.IsEmailvalid = false;
                        ValidationOutput.IsAllValid = false;
                        ValidationOutput.ErrorMsg4Email = "Email格式不正确";
                        return new ValidationResult(false, "Email格式不正确");
                    }
                }
                else
                {
                    ValidationOutput.IsNickNameValid = true;
                    ValidationOutput.IsAllValid = true;
                    return new ValidationResult(true, null);
                }
 
            }
            return new ValidationResult(true, null);
        }
    }
}