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);
|
}
|
}
|
}
|