123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- using System;
- using System.Globalization;
- using System.Text.RegularExpressions;
- using UnityEngine;
- public class RegexUtilities
- {
- bool invalid = false;
-
- public bool IsValidEmail(string strIn)
- {
- invalid = false;
- if (String.IsNullOrEmpty(strIn))
- return false;
- // Use IdnMapping class to convert Unicode domain names.
- try
- {
- strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper,
- RegexOptions.None);
- }
- catch (RegexMatchTimeoutException)
- {
- return false;
- }
- if (invalid)
- return false;
- // Return true if strIn is in valid e-mail format.
- try
- {
- return Regex.IsMatch(strIn,
- @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
- @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
- RegexOptions.IgnoreCase);
- }
- catch (RegexMatchTimeoutException)
- {
- return false;
- }
- }
- public bool IsValidPhone(string Phone)
- {
- try
- {
- char[] ar = new char[]{};
- string number = "";
- switch (LoginManager.Instance.CurStateCountryCodePhone)
- {
- case LoginManager.StateCountryTelephoneCode.Danmark:
- ar = Phone.ToCharArray();
-
- //if (ar[0].ToString() == "7")
- //{
- // if (ar[1].ToString() == "0" || ar[1].ToString() == "2"
- // || ar[1].ToString() == "3" || ar[1].ToString() == "6" || ar[1].ToString() == "9")
- {
- for (int i = 0; i < ar.Length; i++)
- {
- if (Char.IsDigit(ar[i]))
- {
- number += ar[i];
- }
- }
- }
- //}
- if (number.ToCharArray().Length <= 9 && number.ToCharArray().Length >= 8)
- {
- // Debug.Log(number);
- return true;
- }
- return false;
- break;
- case LoginManager.StateCountryTelephoneCode.Ukraine:
- ar = Phone.ToCharArray();
-
- //if (ar[0].ToString() == "0" && ar[1].ToString() == "7")
- {
- //if (ar[2].ToString() == "0" || ar[2].ToString() == "2"
- // || ar[2].ToString() == "3" || ar[2].ToString() == "6" || ar[2].ToString() == "9")
- {
- for (int i = 0; i < ar.Length; i++)
- {
- if (Char.IsDigit(ar[i]))
- {
- number += ar[i];
- }
- }
- }
- }
- if (number.ToCharArray().Length == 10)
- {
- // Debug.Log(number);
- return true;
- }
- return false;
- break;
- case LoginManager.StateCountryTelephoneCode.Iceland:
- ar = Phone.ToCharArray();
- //if (ar[0].ToString() == "0" && ar[1].ToString() == "7")
- {
- //if (ar[2].ToString() == "0" || ar[2].ToString() == "2"
- // || ar[2].ToString() == "3" || ar[2].ToString() == "6" || ar[2].ToString() == "9")
- {
- for (int i = 0; i < ar.Length; i++)
- {
- if (Char.IsDigit(ar[i]))
- {
- number += ar[i];
- }
- }
- }
- }
- if (number.ToCharArray().Length == 7)
- {
- // Debug.Log(number);
- return true;
- }
- return false;
- break;
- }
- }
-
- catch (Exception)
- {
- throw;
- }
- return false;
- }
- /* public bool IsValidNumberPhone(string strIn)
- {
- invalid = false;
- if (String.IsNullOrEmpty(strIn))
- return false;
- if (invalid)
- return false;
- // Return true if strIn is in valid phone number format.
- try
- {
- Debug.Log(Regex.IsMatch(strIn,
- @"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$",
- RegexOptions.IgnoreCase));
- return Regex.IsMatch(strIn,
- @"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$",
- RegexOptions.IgnoreCase);
- }
- catch (RegexMatchTimeoutException)
- {
-
- return false;
- }
- }*/
- private string DomainMapper(Match match)
- {
- // IdnMapping class with default property values.
- IdnMapping idn = new IdnMapping();
- string domainName = match.Groups[2].Value;
- try
- {
- domainName = idn.GetAscii(domainName);
- }
- catch (ArgumentException)
- {
- invalid = true;
- }
- return match.Groups[1].Value + domainName;
- }
- }
- public class RegexMatchTimeoutException : Exception
- {
- }
|