Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Reflection;
- using System.Text.RegularExpressions;
- namespace Sapico.FluentValidation.Helpers
- {
- public class FluentValidationHelpers
- {
- #region -- zakresy numerów (string) --
- ///<summary>
- ///Sprawdzenie czy podane numery (checkStart, checkEnd) zawierają się w dokładnie podanym zakresie (start, end)
- ///</summary>
- public static bool CzyNumeryZwykleMieszczaSieWZakresie(string start, string end, string checkStart, string checkEnd)
- {
- var odNumeru = Int64.Parse(start);
- var doNumeru = Int64.Parse(end);
- var odCheck = Int64.Parse(checkStart);
- var doCheck = Int64.Parse(checkEnd);
- if (odCheck >= odNumeru && doCheck <= doNumeru) //mieści się w zakresie od-do
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- ///<summary>
- ///Sprawdzenie czy podane numery (checkStart, checkEnd) zahaczają, zawierają lub zawierają się w podanym zakresie (start, end)
- ///</summary>
- public static bool CzyNumeryZwykleZahaczajaOZakres(string start, string end, string checkStart, string checkEnd)
- {
- var odNumeru = Int64.Parse(start);
- var doNumeru = Int64.Parse(end);
- var odCheck = Int64.Parse(checkStart);
- var doCheck = Int64.Parse(checkEnd);
- if (odCheck >= odNumeru && doCheck <= doNumeru) //mieści się w zakresie od-do
- {
- return true;
- }
- else if (odCheck <= odNumeru && doCheck >= odNumeru && doCheck <= doNumeru) //zazębia się z przodu
- {
- return true;
- }
- else if (odCheck >= odNumeru && odCheck <= doNumeru && doCheck >= doNumeru) //zazębia się z tyłu
- {
- return true;
- }
- else if (odCheck <= odNumeru && doCheck >= doNumeru) //zawiera cały zakres
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- #endregion
- #region -- daty --
- ///<summary>
- ///Sprawdzenie czy podane daty (odCheck, doCheck) zahaczają, zawierają lub zawierają się w podanym zakresie (start, end)
- ///</summary>
- public static bool CzyDatyZahaczajaOZakres(DateTime start, DateTime? end, DateTime odCheck, DateTime? doCheck)
- {
- if (doCheck == null) doCheck = DateTime.MaxValue;
- if (end == null) end = DateTime.MaxValue;
- if (odCheck >= start && doCheck <= end) //mieści się w zakresie od-do
- {
- return true;
- }
- else if (odCheck <= start && doCheck >= start && doCheck <= end) //zazębia się z przodu
- {
- return true;
- }
- else if (odCheck >= start && odCheck <= end && doCheck >= end) //zazębia się z tyłu
- {
- return true;
- }
- else if (odCheck <= start && doCheck >= end) //zawiera cały zakres
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// Sprawdzenie czy podana data mieści się w zakresie dat
- /// </summary>
- /// <returns></returns>
- public static bool CzyDataWZakresie(DateTime start, DateTime? end, DateTime data)
- {
- if (end == null && data >= start)
- {
- return true;
- }
- else if (data >= start && data <= end)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// Sprawdzenie czy data DO nie jest wcześniejsza, niż data OD
- /// </summary>
- /// <returns></returns>
- public static bool CzyDataOdDoOK(DateTime? start, DateTime? end)
- {
- if (end == null)
- {
- return true;
- }
- else if (start == null && end == null)
- {
- return true;
- }
- else if (start <= end)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// Sprawdzenie czy podana data jest większa lub równa minimalnej
- /// </summary>
- /// <param name="minimal"></param>
- /// <param name="dataCheck"></param>
- /// <returns></returns>
- public static bool CzyDataMinimalna(DateTime minimal, DateTime? dataCheck)
- {
- if (dataCheck == null)
- {
- return true;
- }
- if (dataCheck >= minimal)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- #endregion
- #region -- znaki specjalne --
- /// <summary>
- /// Sprawdzenie czy podany string nie zawiera znaków specjalnych
- /// Dopuszcza tylko:
- /// a) cyfry,
- /// b) duże i małe litery,
- /// c) kropki
- /// d) podkreślenia
- /// BEZ SPACJI
- /// </summary>
- /// <returns></returns>
- public static bool CzyNieMaZnakowSpecjalnych(string check)
- {
- var regexItem = new Regex("^[a-zA-Z0-9_.]*$");
- if (regexItem.IsMatch(check))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- #endregion
- public static bool PeselValidator(string pesel)
- {
- if (string.IsNullOrEmpty(pesel)) return true;
- if (pesel.Length != 11)
- return false;
- int[] weight = { 1, 3, 7, 9, 1, 3, 7, 9, 1, 3 };
- string validation = "0123456789";
- int peselsum = 0;
- bool result = false;
- if (pesel != null)
- {
- for (int idx = 0; idx < 10; idx++)
- peselsum += (validation.IndexOf(pesel.Substring(idx, 1))) * weight[idx];
- peselsum = ((10 - (peselsum % 10)) % 10);
- if (peselsum == validation.IndexOf(pesel.Substring(10, 1)))
- result = true;
- }
- return result ? true : false;
- }
- public static bool NipValidator(string nip)
- {
- if (string.IsNullOrEmpty(nip)) return true;
- bool result = false;
- if (nip != null)
- {
- string nipValue = "";
- for (int idx = 0; idx < nip.Length; idx++)
- if (nip.Substring(idx, 1) != "-" && nip.Substring(idx, 1) != " ")
- nipValue += nip.Substring(idx, 1);
- if ((nipValue.Length == 10))
- {
- int nipSum = 0;
- string validation = "0123456789";
- int[] weight = { 6, 5, 7, 2, 3, 4, 5, 6, 7 };
- for (var idx = 0; idx < 9; idx++)
- nipSum += (validation.IndexOf(nipValue.Substring(idx, 1))) * weight[idx];
- nipSum = (nipSum % 11);
- if (nipSum == validation.IndexOf(nipValue.Substring(9, 1)))
- result = true;
- }
- return result ? true : false;
- }
- return result;
- }
- public static bool CellValidator(string number)
- {
- if (string.IsNullOrEmpty(number)) return true;
- // if ((number.Length > 17) || (number.Length < 9)) return false;
- return ((Regex.Match(number, @"\(?\d{3}\)?-? *\d{3}-? *-?\d{3}").Success) /*|| (Regex.Match(number, @"(+48)* *[0-9]{3}-[0-9]{3}-[0-9]{3}").Success) || (Regex.Match(number, @"(+48)* *[0-9]{3} [0-9]{3} [0-9]{3}").Success) || (Regex.Match(number, @"(+48)* *([0-9]{3}) [0-9]{3} [0-9]{3}").Success) || (Regex.Match(number, @"(+48)* *([0-9]{3})-[0-9]{3}-[0-9]{3}").Success) || (Regex.Match(number, @"(+48)* *([0-9]{3}) [0-9]{6}").Success)*/);
- }
- public static bool StationaryValidator(string number)
- {
- if (string.IsNullOrEmpty(number)) return true;
- // if ((number.Length > 17) || (number.Length < 9)) return false;
- return Regex.Match(number, @"\(?\d{3}\)?-? *\d{3}-? *-?\d{3}").Success;
- }
- public static bool EmailValidator(string email)
- {
- if (string.IsNullOrEmpty(email)) return true;
- return Regex.Match(email, @"[A-Z0-9._-]+@[A-Z0-9.-]+\.[A-Z]{2,}").Success;
- }
- public static bool CodeValidator(string code)
- {
- if (string.IsNullOrEmpty(code)) return true;
- if (code.Length != 6)
- return false;
- return Regex.Match(code, @"[0-9]{2}-[0-9]{3}").Success;
- }
- public static bool DowodValidator(string id)
- {
- if (string.IsNullOrEmpty(id)) return true;
- if (!Regex.Match(id, @"[A-Z]{3}[0-9]{6}").Success)
- return false;
- else
- {
- Dictionary<char, int> dict =
- new Dictionary<char, int>();
- int value = 10, sum = 0;
- for (int letter = 65; letter < 92; letter++)
- {
- dict.Add((char)letter, value);
- value++;
- }
- int[] weight = { 7, 3, 1, 7, 3, 1, 7, 3 };
- for (int i = 0; i < 9; i++)
- {
- if (i < 3)
- {
- sum += dict[id[i]]*weight[i];
- }
- else
- if (i >= 4)
- {
- sum += (int)Char.GetNumericValue((id[i])) * weight[i - 1];
- }
- }
- if (sum % 10 == (int)Char.GetNumericValue(id[3]))
- return true;
- else
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement