BaseCompareValidator.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: BaseCompareValidator
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Implementation: yes
  8. * Contact: <[email protected]>
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.Collections;
  15. using System.Globalization;
  16. using System.Text;
  17. using System.Text.RegularExpressions;
  18. using System.Web;
  19. using System.Web.UI;
  20. namespace System.Web.UI.WebControls
  21. {
  22. public abstract class BaseCompareValidator: BaseValidator
  23. {
  24. protected BaseCompareValidator(): base()
  25. {
  26. }
  27. public static bool CanConvert(string text, ValidationDataType type)
  28. {
  29. object o = null;
  30. return Convert(text, type, out o);
  31. }
  32. public ValidationDataType Type
  33. {
  34. get
  35. {
  36. object o = ViewState["Type"];
  37. if(o!=null)
  38. return (ValidationDataType)o;
  39. return ValidationDataType.String;
  40. }
  41. set
  42. {
  43. if(!System.Enum.IsDefined(typeof(ValidationDataType), value))
  44. throw new ArgumentException();
  45. ViewState["Type"] = value;
  46. }
  47. }
  48. protected static int CutoffYear
  49. {
  50. get
  51. {
  52. return DateTimeFormatInfo.CurrentInfo.Calendar.TwoDigitYearMax;
  53. }
  54. }
  55. protected static int GetFullYear(int shortYear)
  56. {
  57. int century = DateTime.Today.Year - (DateTime.Today.Year % 100);
  58. if(century < CutoffYear)
  59. {
  60. return (shortYear + century);
  61. }
  62. return (shortYear + century - 100);
  63. }
  64. protected override void AddAttributesToRender(HtmlTextWriter writer)
  65. {
  66. base.AddAttributesToRender(writer);
  67. if(RenderUplevel)
  68. {
  69. writer.AddAttribute("type", PropertyConverter.EnumToString(typeof(ValidationDataType), Type));
  70. NumberFormatInfo currInfo = NumberFormatInfo.CurrentInfo;
  71. if(Type == ValidationDataType.Double)
  72. {
  73. writer.AddAttribute("decimalchar", currInfo.NumberDecimalSeparator);
  74. return;
  75. }
  76. if(Type == ValidationDataType.Currency)
  77. {
  78. writer.AddAttribute("decimalchar", currInfo.CurrencyDecimalSeparator);
  79. string grpSep = currInfo.CurrencyGroupSeparator;
  80. if(grpSep[0] == 0xA0)
  81. {
  82. grpSep = " ";
  83. }
  84. writer.AddAttribute("groupchar", grpSep);
  85. writer.AddAttribute("digits", currInfo.CurrencyDecimalDigits.ToString(NumberFormatInfo.InvariantInfo));
  86. return;
  87. }
  88. if(Type == ValidationDataType.Date)
  89. {
  90. writer.AddAttribute("cutoffyear", CutoffYear.ToString());
  91. writer.AddAttribute("century", ( DateTime.Today.Year - (DateTime.Today.Year % 100) ).ToString());
  92. return;
  93. }
  94. }
  95. }
  96. protected override bool DetermineRenderUplevel()
  97. {
  98. if(Type == ValidationDataType.Date && DateTimeFormatInfo.CurrentInfo.Calendar.GetType() != typeof(GregorianCalendar))
  99. {
  100. return false;
  101. }
  102. return base.DetermineRenderUplevel();
  103. }
  104. /// <summary>
  105. /// Undocumented
  106. /// </summary>
  107. protected static bool Compare(string leftText, string rightText, ValidationCompareOperator op, ValidationDataType type)
  108. {
  109. object left = null, right = null;
  110. if(!Convert(leftText, type, out left))
  111. {
  112. return false;
  113. }
  114. if(op == ValidationCompareOperator.DataTypeCheck)
  115. {
  116. return true;
  117. }
  118. if(!Convert(rightText, type, out right))
  119. {
  120. return true;
  121. }
  122. int compareResult = 0;
  123. switch(type)
  124. {
  125. case ValidationDataType.String:
  126. compareResult = ((String)left).CompareTo(right);
  127. break;
  128. case ValidationDataType.Integer:
  129. compareResult = ((int)left).CompareTo(right);
  130. break;
  131. case ValidationDataType.Double:
  132. compareResult = ((Double)left).CompareTo(right);
  133. break;
  134. case ValidationDataType.Date:
  135. compareResult = ((DateTime)left).CompareTo(right);
  136. break;
  137. case ValidationDataType.Currency:
  138. compareResult = ((Decimal)left).CompareTo(right);
  139. break;
  140. }
  141. switch(op)
  142. {
  143. case ValidationCompareOperator.Equal:
  144. return (compareResult == 0);
  145. case ValidationCompareOperator.NotEqual:
  146. return (compareResult != 0);
  147. case ValidationCompareOperator.GreaterThan:
  148. return (compareResult > 0);
  149. case ValidationCompareOperator.GreaterThanEqual:
  150. return (compareResult >= 0);
  151. case ValidationCompareOperator.LessThan:
  152. return (compareResult < 0);
  153. case ValidationCompareOperator.LessThanEqual:
  154. return (compareResult == 0);
  155. }
  156. return false;
  157. }
  158. /// <summary>
  159. /// Undocumented
  160. /// </summary>
  161. protected static string GetDateElementOrder()
  162. {
  163. string pattern = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
  164. //TODO: What are the various possibilities?
  165. // I can think of only y*/M*/d*, d*/M*/y*, M*/d*/y*
  166. if(pattern.IndexOf('y') < pattern.IndexOf('M'))
  167. {
  168. return "ymd";
  169. }
  170. if(pattern.IndexOf('M') < pattern.IndexOf('d'))
  171. {
  172. return "mdy";
  173. }
  174. return "dmy";
  175. }
  176. /// <summary>
  177. /// Undocumented
  178. /// </summary>
  179. protected static bool Convert(string text, ValidationDataType type, out object convertedValue)
  180. {
  181. convertedValue = null;
  182. try
  183. {
  184. switch(type)
  185. {
  186. case ValidationDataType.String: convertedValue = text;
  187. break;
  188. case ValidationDataType.Integer: convertedValue = Int32.Parse(text, CultureInfo.InvariantCulture);
  189. break;
  190. case ValidationDataType.Double:
  191. Match matchDouble = Regex.Match(text, @"^\s*([-\+])?(\d+)?(\"
  192. + NumberFormatInfo.CurrentInfo.NumberDecimalSeparator
  193. + @"(\d+))?\s*$");
  194. if(matchDouble.Success)
  195. {
  196. string sign = (matchDouble.Groups[1].Success ? matchDouble.Groups[1].Value : "+");
  197. string decPart = (matchDouble.Groups[2].Success ? matchDouble.Groups[2].Value : "0");
  198. string mantissa = (matchDouble.Groups[4].Success ? matchDouble.Groups[4].Value : "0");
  199. convertedValue = Double.Parse(sign + decPart + "." + mantissa, CultureInfo.InvariantCulture);
  200. }
  201. break;
  202. case ValidationDataType.Date:
  203. if(DateTimeFormatInfo.CurrentInfo.Calendar.GetType() != typeof(GregorianCalendar))
  204. {
  205. convertedValue = DateTime.Parse(text);
  206. break;
  207. }
  208. string order = GetDateElementOrder();
  209. int date = 0, mth = 0, year = 0;
  210. Match matchDate = Regex.Match(text, @"^\s*((\d{4})|(\d{2}))([\.\/-])(\d{1,2})\4(\d{1,2})\s*$");
  211. if(matchDate.Success && order == "ymd")
  212. {
  213. date = Int32.Parse(matchDate.Groups[6].Value, CultureInfo.InvariantCulture);
  214. mth = Int32.Parse(matchDate.Groups[5].Value, CultureInfo.InvariantCulture);
  215. year = Int32.Parse((matchDate.Groups[2].Success ? matchDate.Groups[2].Value : matchDate.Groups[3].Value), CultureInfo.InvariantCulture);
  216. } else
  217. {
  218. matchDate = Regex.Match(text, @"^\s*(\d{1,2})([\.\/-])(\d{1,2})\2((\d{4}|\d{2}))\s*$");
  219. if(matchDate.Success)
  220. {
  221. if(order == "dmy")
  222. {
  223. date = Int32.Parse(matchDate.Groups[1].Value, CultureInfo.InvariantCulture);
  224. mth = Int32.Parse(matchDate.Groups[3].Value, CultureInfo.InvariantCulture);
  225. year = Int32.Parse((matchDate.Groups[5].Success ? matchDate.Groups[5].Value : matchDate.Groups[6].Value), CultureInfo.InvariantCulture);
  226. }
  227. if(order == "mdy")
  228. {
  229. date = Int32.Parse(matchDate.Groups[3].Value, CultureInfo.InvariantCulture);
  230. mth = Int32.Parse(matchDate.Groups[1].Value, CultureInfo.InvariantCulture);
  231. year = Int32.Parse((matchDate.Groups[5].Success ? matchDate.Groups[5].Value : matchDate.Groups[6].Value), CultureInfo.InvariantCulture);
  232. }
  233. }
  234. }
  235. year = (year < 100 ? GetFullYear(year) : year);
  236. if(matchDate.Success && date!=0 && mth!=0 && year!=0)
  237. {
  238. convertedValue = new DateTime(year, mth, date);
  239. }
  240. break;
  241. case ValidationDataType.Currency:
  242. string decSep = NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator;
  243. string grpSep = NumberFormatInfo.CurrentInfo.CurrencyGroupSeparator;
  244. int decDig = NumberFormatInfo.CurrentInfo.CurrencyDecimalDigits;
  245. if(grpSep[0] == 0xA0)
  246. {
  247. grpSep = " ";
  248. }
  249. string[] patternArray = new string[5];
  250. patternArray[0] = "^\\s*([-\\+])?(((\\d+)\\";
  251. patternArray[1] = grpSep;
  252. patternArray[2] = @")*)(\d+)";
  253. if(decDig > 0)
  254. {
  255. string[] decPattern = new string[5];
  256. decPattern[0] = "(\\";
  257. decPattern[1] = decSep;
  258. decPattern[2] = @"(\d{1,";
  259. decPattern[3] = decDig.ToString(NumberFormatInfo.InvariantInfo);
  260. decPattern[4] = @"}))";
  261. patternArray[3] = String.Concat(decPattern);
  262. } else
  263. {
  264. patternArray[3] = String.Empty;
  265. }
  266. patternArray[4] = @"?\s*$";
  267. Match matchCurrency = Regex.Match(text, String.Concat(patternArray));
  268. if(matchCurrency.Success)
  269. {
  270. StringBuilder sb = new StringBuilder();
  271. sb.Append(matchCurrency.Groups[1]);
  272. CaptureCollection cc = matchCurrency.Groups[4].Captures;
  273. foreach(IEnumerable current in cc)
  274. {
  275. sb.Append((Capture)current);
  276. }
  277. sb.Append(matchCurrency.Groups[5]);
  278. if(decDig > 0)
  279. {
  280. sb.Append(".");
  281. sb.Append(matchCurrency.Groups[7]);
  282. }
  283. convertedValue = Decimal.Parse(sb.ToString(), CultureInfo.InvariantCulture);
  284. }
  285. break;
  286. }
  287. } catch(Exception e)
  288. {
  289. convertedValue = null;
  290. }
  291. return (convertedValue != null);
  292. }
  293. }
  294. }