BaseCompareValidator.cs 9.5 KB

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