Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. using System.Text.RegularExpressions;
  7.  
  8.  
  9. namespace Sapico.FluentValidation.Helpers
  10. {
  11.     public class FluentValidationHelpers
  12.     {
  13.         #region -- zakresy numerów (string) --
  14.  
  15.         ///<summary>
  16.         ///Sprawdzenie czy podane numery (checkStart, checkEnd) zawierają się w dokładnie podanym zakresie (start, end)
  17.         ///</summary>
  18.         public static bool CzyNumeryZwykleMieszczaSieWZakresie(string start, string end, string checkStart, string checkEnd)
  19.         {
  20.             var odNumeru = Int64.Parse(start);
  21.             var doNumeru = Int64.Parse(end);
  22.             var odCheck = Int64.Parse(checkStart);
  23.             var doCheck = Int64.Parse(checkEnd);
  24.  
  25.             if (odCheck >= odNumeru && doCheck <= doNumeru) //mieści się w zakresie od-do
  26.             {
  27.                 return true;
  28.             }
  29.             else
  30.             {
  31.                 return false;
  32.             }
  33.         }
  34.  
  35.         ///<summary>
  36.         ///Sprawdzenie czy podane numery (checkStart, checkEnd) zahaczają, zawierają lub zawierają się w podanym zakresie (start, end)
  37.         ///</summary>
  38.         public static bool CzyNumeryZwykleZahaczajaOZakres(string start, string end, string checkStart, string checkEnd)
  39.         {
  40.             var odNumeru = Int64.Parse(start);
  41.             var doNumeru = Int64.Parse(end);
  42.             var odCheck = Int64.Parse(checkStart);
  43.             var doCheck = Int64.Parse(checkEnd);
  44.  
  45.             if (odCheck >= odNumeru && doCheck <= doNumeru) //mieści się w zakresie od-do
  46.             {
  47.                 return true;
  48.             }
  49.             else if (odCheck <= odNumeru && doCheck >= odNumeru && doCheck <= doNumeru) //zazębia się z przodu
  50.             {
  51.                 return true;
  52.             }
  53.             else if (odCheck >= odNumeru && odCheck <= doNumeru && doCheck >= doNumeru) //zazębia się z tyłu
  54.             {
  55.                 return true;
  56.             }
  57.             else if (odCheck <= odNumeru && doCheck >= doNumeru) //zawiera cały zakres
  58.             {
  59.                 return true;
  60.             }
  61.             else
  62.             {
  63.                 return false;
  64.             }
  65.         }
  66.  
  67.         #endregion
  68.  
  69.         #region -- daty --
  70.  
  71.         ///<summary>
  72.         ///Sprawdzenie czy podane daty (odCheck, doCheck) zahaczają, zawierają lub zawierają się w podanym zakresie (start, end)
  73.         ///</summary>
  74.         public static bool CzyDatyZahaczajaOZakres(DateTime start, DateTime? end, DateTime odCheck, DateTime? doCheck)
  75.         {
  76.             if (doCheck == null) doCheck = DateTime.MaxValue;
  77.             if (end == null) end = DateTime.MaxValue;
  78.  
  79.             if (odCheck >= start && doCheck <= end) //mieści się w zakresie od-do
  80.             {
  81.                 return true;
  82.             }
  83.             else if (odCheck <= start && doCheck >= start && doCheck <= end) //zazębia się z przodu
  84.             {
  85.                 return true;
  86.             }
  87.             else if (odCheck >= start && odCheck <= end && doCheck >= end) //zazębia się z tyłu
  88.             {
  89.                 return true;
  90.             }
  91.             else if (odCheck <= start && doCheck >= end) //zawiera cały zakres
  92.             {
  93.                 return true;
  94.             }
  95.             else
  96.             {
  97.                 return false;
  98.             }
  99.         }
  100.  
  101.         /// <summary>
  102.         /// Sprawdzenie czy podana data mieści się w zakresie dat
  103.         /// </summary>
  104.         /// <returns></returns>
  105.         public static bool CzyDataWZakresie(DateTime start, DateTime? end, DateTime data)
  106.         {
  107.             if (end == null && data >= start)
  108.             {
  109.                 return true;
  110.             }
  111.             else if (data >= start && data <= end)
  112.             {
  113.                 return true;
  114.             }
  115.             else
  116.             {
  117.                 return false;
  118.             }
  119.         }
  120.  
  121.         /// <summary>
  122.         /// Sprawdzenie czy data DO nie jest wcześniejsza, niż data OD
  123.         /// </summary>
  124.         /// <returns></returns>
  125.         public static bool CzyDataOdDoOK(DateTime? start, DateTime? end)
  126.         {
  127.             if (end == null)
  128.             {
  129.                 return true;
  130.             }
  131.             else if (start == null && end == null)
  132.             {
  133.                 return true;
  134.             }
  135.             else if (start <= end)
  136.             {
  137.                 return true;
  138.             }
  139.             else
  140.             {
  141.                 return false;
  142.             }
  143.         }
  144.  
  145.         /// <summary>
  146.         /// Sprawdzenie czy podana data jest większa lub równa minimalnej
  147.         /// </summary>
  148.         /// <param name="minimal"></param>
  149.         /// <param name="dataCheck"></param>
  150.         /// <returns></returns>
  151.         public static bool CzyDataMinimalna(DateTime minimal, DateTime? dataCheck)
  152.         {
  153.             if (dataCheck == null)
  154.             {
  155.                 return true;
  156.             }
  157.             if (dataCheck >= minimal)
  158.             {
  159.                 return true;
  160.             }
  161.             else
  162.             {
  163.                 return false;
  164.             }
  165.         }
  166.  
  167.         #endregion
  168.  
  169.         #region -- znaki specjalne --
  170.  
  171.         /// <summary>
  172.         /// Sprawdzenie czy podany string nie zawiera znaków specjalnych
  173.         /// Dopuszcza tylko:
  174.         /// a) cyfry,
  175.         /// b) duże i małe litery,
  176.         /// c) kropki
  177.         /// d) podkreślenia
  178.         /// BEZ SPACJI
  179.         /// </summary>
  180.         /// <returns></returns>
  181.         public static bool CzyNieMaZnakowSpecjalnych(string check)
  182.         {
  183.             var regexItem = new Regex("^[a-zA-Z0-9_.]*$");
  184.  
  185.             if (regexItem.IsMatch(check))
  186.             {
  187.                 return true;
  188.             }
  189.             else
  190.             {
  191.                 return false;
  192.             }
  193.         }
  194.  
  195.         #endregion
  196.         public static bool PeselValidator(string pesel)
  197.         {
  198.             if (string.IsNullOrEmpty(pesel)) return true;
  199.  
  200.             if (pesel.Length != 11)
  201.                 return false;
  202.             int[] weight = { 1, 3, 7, 9, 1, 3, 7, 9, 1, 3 };
  203.             string validation = "0123456789";
  204.             int peselsum = 0;
  205.             bool result = false;
  206.             if (pesel != null)
  207.             {
  208.                 for (int idx = 0; idx < 10; idx++)
  209.                     peselsum += (validation.IndexOf(pesel.Substring(idx, 1))) * weight[idx];
  210.                 peselsum = ((10 - (peselsum % 10)) % 10);
  211.                 if (peselsum == validation.IndexOf(pesel.Substring(10, 1)))
  212.                     result = true;
  213.             }
  214.             return result ? true : false;
  215.         }
  216.  
  217.         public static bool NipValidator(string nip)
  218.         {
  219.             if (string.IsNullOrEmpty(nip)) return true;
  220.             bool result = false;
  221.             if (nip != null)
  222.             {
  223.                 string nipValue = "";
  224.                 for (int idx = 0; idx < nip.Length; idx++)
  225.                     if (nip.Substring(idx, 1) != "-" && nip.Substring(idx, 1) != " ")
  226.                         nipValue += nip.Substring(idx, 1);
  227.  
  228.                 if ((nipValue.Length == 10))
  229.                 {
  230.                     int nipSum = 0;
  231.                     string validation = "0123456789";
  232.                     int[] weight = { 6, 5, 7, 2, 3, 4, 5, 6, 7 };
  233.                     for (var idx = 0; idx < 9; idx++)
  234.                         nipSum += (validation.IndexOf(nipValue.Substring(idx, 1))) * weight[idx];
  235.                     nipSum = (nipSum % 11);
  236.                     if (nipSum == validation.IndexOf(nipValue.Substring(9, 1)))
  237.                         result = true;
  238.                 }
  239.                 return result ? true : false;
  240.             }
  241.             return result;
  242.         }
  243.  
  244.         public static bool CellValidator(string number)
  245.         {
  246.             if (string.IsNullOrEmpty(number)) return true;
  247.            // if ((number.Length > 17) || (number.Length < 9)) return false;
  248.             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)*/);
  249.         }
  250.  
  251.         public static bool StationaryValidator(string number)
  252.         {
  253.             if (string.IsNullOrEmpty(number)) return true;
  254.           //  if ((number.Length > 17) || (number.Length < 9)) return false;
  255.             return Regex.Match(number, @"\(?\d{3}\)?-? *\d{3}-? *-?\d{3}").Success;
  256.         }
  257.  
  258.         public static bool EmailValidator(string email)
  259.         {
  260.             if (string.IsNullOrEmpty(email)) return true;
  261.             return Regex.Match(email, @"[A-Z0-9._-]+@[A-Z0-9.-]+\.[A-Z]{2,}").Success;
  262.         }
  263.  
  264.         public static bool CodeValidator(string code)
  265.         {
  266.             if (string.IsNullOrEmpty(code)) return true;
  267.             if (code.Length != 6)
  268.                 return false;
  269.             return Regex.Match(code, @"[0-9]{2}-[0-9]{3}").Success;
  270.         }
  271.  
  272.         public static bool DowodValidator(string id)
  273.         {
  274.             if (string.IsNullOrEmpty(id)) return true;
  275.             if (!Regex.Match(id, @"[A-Z]{3}[0-9]{6}").Success)
  276.                 return false;
  277.             else
  278.             {
  279.                 Dictionary<char, int> dict =
  280.                     new Dictionary<char, int>();
  281.                 int value = 10, sum = 0;
  282.                 for (int letter = 65; letter < 92; letter++)
  283.                 {
  284.                     dict.Add((char)letter, value);
  285.                     value++;
  286.                 }
  287.                 int[] weight = { 7, 3, 1, 7, 3, 1, 7, 3 };
  288.  
  289.                 for (int i = 0; i < 9; i++)
  290.                 {
  291.                     if (i < 3)
  292.                     {
  293.                         sum += dict[id[i]]*weight[i];
  294.                     }
  295.                     else
  296.                     if (i >= 4)
  297.                     {
  298.                         sum += (int)Char.GetNumericValue((id[i])) * weight[i - 1];
  299.                     }
  300.                 }
  301.                 if (sum % 10 == (int)Char.GetNumericValue(id[3]))
  302.                     return true;
  303.                 else
  304.                     return false;
  305.             }
  306.         }
  307.  
  308.  
  309.     }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement