-/**\r
- * Namespace: System.Web.UI.WebControls\r
- * Class: BaseCompareValidator\r
- *\r
- * Author: Gaurav Vaish\r
- * Maintainer: gvaish@iitk.ac.in\r
- * Implementation: yes\r
- * Contact: <gvaish@iitk.ac.in>\r
- * Status: 100%\r
- *\r
- * (C) Gaurav Vaish (2001)\r
- */\r
-\r
-using System;\r
-using System.Collections;\r
-using System.Globalization;\r
-using System.Text;\r
-using System.Text.RegularExpressions;\r
-using System.Web;\r
-using System.Web.UI;\r
-using System.ComponentModel;\r
-\r
-namespace System.Web.UI.WebControls\r
-{\r
- public abstract class BaseCompareValidator: BaseValidator\r
- {\r
- protected BaseCompareValidator(): base()\r
- {\r
- }\r
-\r
- public static bool CanConvert(string text, ValidationDataType type)\r
- {\r
- object o = null;\r
- return Convert(text, type, out o);\r
- }\r
-\r
- [DefaultValue(ValidationDataType.String)]\r
- [WebCategory("Behaviour")]\r
- [WebSysDescription("RangeValidator_Type")]\r
- public ValidationDataType Type\r
- {\r
- get\r
- {\r
- object o = ViewState["Type"];\r
- if(o!=null)\r
- return (ValidationDataType)o;\r
- return ValidationDataType.String;\r
- }\r
- set\r
- {\r
- if(!System.Enum.IsDefined(typeof(ValidationDataType), value))\r
- throw new ArgumentException();\r
- ViewState["Type"] = value;\r
- }\r
- }\r
-\r
- protected static int CutoffYear\r
- {\r
- get\r
- {\r
- return DateTimeFormatInfo.CurrentInfo.Calendar.TwoDigitYearMax;\r
- }\r
- }\r
-\r
- protected static int GetFullYear(int shortYear)\r
- {\r
- int century = DateTime.Today.Year - (DateTime.Today.Year % 100);\r
- if(century < CutoffYear)\r
- {\r
- return (shortYear + century);\r
- }\r
- return (shortYear + century - 100);\r
- }\r
-\r
- protected override void AddAttributesToRender(HtmlTextWriter writer)\r
- {\r
- base.AddAttributesToRender(writer);\r
- if(RenderUplevel)\r
- {\r
- writer.AddAttribute("type", PropertyConverter.EnumToString(typeof(ValidationDataType), Type));\r
- NumberFormatInfo currInfo = NumberFormatInfo.CurrentInfo;\r
- if(Type == ValidationDataType.Double)\r
- {\r
- writer.AddAttribute("decimalchar", currInfo.NumberDecimalSeparator);\r
- return;\r
- }\r
- if(Type == ValidationDataType.Currency)\r
- {\r
- writer.AddAttribute("decimalchar", currInfo.CurrencyDecimalSeparator);\r
- string grpSep = currInfo.CurrencyGroupSeparator;\r
- if(grpSep[0] == 0xA0)\r
- {\r
- grpSep = " ";\r
- }\r
- writer.AddAttribute("groupchar", grpSep);\r
- writer.AddAttribute("digits", currInfo.CurrencyDecimalDigits.ToString(NumberFormatInfo.InvariantInfo));\r
- return;\r
- }\r
- if(Type == ValidationDataType.Date)\r
- {\r
- writer.AddAttribute("cutoffyear", CutoffYear.ToString());\r
- writer.AddAttribute("century", ( DateTime.Today.Year - (DateTime.Today.Year % 100) ).ToString());\r
- return;\r
- }\r
- }\r
- }\r
-\r
- protected override bool DetermineRenderUplevel()\r
- {\r
- if(Type == ValidationDataType.Date && DateTimeFormatInfo.CurrentInfo.Calendar.GetType() != typeof(GregorianCalendar))\r
- {\r
- return false;\r
- }\r
- return base.DetermineRenderUplevel();\r
- }\r
-\r
- /// <summary>\r
- /// Undocumented\r
- /// </summary>\r
- protected static bool Compare(string leftText, string rightText, ValidationCompareOperator op, ValidationDataType type)\r
- {\r
- object left = null, right = null;\r
- if(!Convert(leftText, type, out left))\r
- {\r
- return false;\r
- }\r
- if(op == ValidationCompareOperator.DataTypeCheck)\r
- {\r
- return true;\r
- }\r
- if(!Convert(rightText, type, out right))\r
- {\r
- return true;\r
- }\r
- int compareResult = 0;\r
- switch(type)\r
- {\r
- case ValidationDataType.String:\r
- compareResult = ((String)left).CompareTo(right);\r
- break;\r
- case ValidationDataType.Integer:\r
- compareResult = ((int)left).CompareTo(right);\r
- break;\r
- case ValidationDataType.Double:\r
- compareResult = ((Double)left).CompareTo(right);\r
- break;\r
- case ValidationDataType.Date:\r
- compareResult = ((DateTime)left).CompareTo(right);\r
- break;\r
- case ValidationDataType.Currency:\r
- compareResult = ((Decimal)left).CompareTo(right);\r
- break;\r
- }\r
- switch(op)\r
- {\r
- case ValidationCompareOperator.Equal:\r
- return (compareResult == 0);\r
- case ValidationCompareOperator.NotEqual:\r
- return (compareResult != 0);\r
- case ValidationCompareOperator.GreaterThan:\r
- return (compareResult > 0);\r
- case ValidationCompareOperator.GreaterThanEqual:\r
- return (compareResult >= 0);\r
- case ValidationCompareOperator.LessThan:\r
- return (compareResult < 0);\r
- case ValidationCompareOperator.LessThanEqual:\r
- return (compareResult == 0);\r
- }\r
- return false;\r
- }\r
-\r
- /// <summary>\r
- /// Undocumented\r
- /// </summary>\r
- protected static string GetDateElementOrder()\r
- {\r
- string pattern = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;\r
-\r
- //TODO: What are the various possibilities?\r
- // I can think of only y*/M*/d*, d*/M*/y*, M*/d*/y*\r
- if(pattern.IndexOf('y') < pattern.IndexOf('M'))\r
- {\r
- return "ymd";\r
- }\r
- if(pattern.IndexOf('M') < pattern.IndexOf('d'))\r
- {\r
- return "mdy";\r
- }\r
- return "dmy";\r
- }\r
-\r
- /// <summary>\r
- /// Undocumented\r
- /// </summary>\r
- protected static bool Convert(string text, ValidationDataType type, out object convertedValue)\r
- {\r
- convertedValue = null;\r
- try\r
- {\r
- switch(type)\r
- {\r
- case ValidationDataType.String: convertedValue = text;\r
- break;\r
- case ValidationDataType.Integer: convertedValue = Int32.Parse(text, CultureInfo.InvariantCulture);\r
- break;\r
- case ValidationDataType.Double:\r
- Match matchDouble = Regex.Match(text, @"^\s*([-\+])?(\d+)?(\"\r
- + NumberFormatInfo.CurrentInfo.NumberDecimalSeparator\r
- + @"(\d+))?\s*$");\r
- if(matchDouble.Success)\r
- {\r
- string sign = (matchDouble.Groups[1].Success ? matchDouble.Groups[1].Value : "+");\r
- string decPart = (matchDouble.Groups[2].Success ? matchDouble.Groups[2].Value : "0");\r
- string mantissa = (matchDouble.Groups[4].Success ? matchDouble.Groups[4].Value : "0");\r
- convertedValue = Double.Parse(sign + decPart + "." + mantissa, CultureInfo.InvariantCulture);\r
- }\r
- break;\r
- case ValidationDataType.Date:\r
- if(DateTimeFormatInfo.CurrentInfo.Calendar.GetType() != typeof(GregorianCalendar))\r
- {\r
- convertedValue = DateTime.Parse(text);\r
- break;\r
- }\r
- string order = GetDateElementOrder();\r
- int date = 0, mth = 0, year = 0;\r
- Match matchDate = Regex.Match(text, @"^\s*((\d{4})|(\d{2}))([\.\/-])(\d{1,2})\4(\d{1,2})\s*$");\r
- if(matchDate.Success && order == "ymd")\r
- {\r
- date = Int32.Parse(matchDate.Groups[6].Value, CultureInfo.InvariantCulture);\r
- mth = Int32.Parse(matchDate.Groups[5].Value, CultureInfo.InvariantCulture);\r
- year = Int32.Parse((matchDate.Groups[2].Success ? matchDate.Groups[2].Value : matchDate.Groups[3].Value), CultureInfo.InvariantCulture);\r
- } else\r
- {\r
- matchDate = Regex.Match(text, @"^\s*(\d{1,2})([\.\/-])(\d{1,2})\2((\d{4}|\d{2}))\s*$");\r
- if(matchDate.Success)\r
- {\r
- if(order == "dmy")\r
- {\r
- date = Int32.Parse(matchDate.Groups[1].Value, CultureInfo.InvariantCulture);\r
- mth = Int32.Parse(matchDate.Groups[3].Value, CultureInfo.InvariantCulture);\r
- year = Int32.Parse((matchDate.Groups[5].Success ? matchDate.Groups[5].Value : matchDate.Groups[6].Value), CultureInfo.InvariantCulture);\r
- }\r
- if(order == "mdy")\r
- {\r
- date = Int32.Parse(matchDate.Groups[3].Value, CultureInfo.InvariantCulture);\r
- mth = Int32.Parse(matchDate.Groups[1].Value, CultureInfo.InvariantCulture);\r
- year = Int32.Parse((matchDate.Groups[5].Success ? matchDate.Groups[5].Value : matchDate.Groups[6].Value), CultureInfo.InvariantCulture);\r
- }\r
- }\r
- }\r
- year = (year < 100 ? GetFullYear(year) : year);\r
- if(matchDate.Success && date!=0 && mth!=0 && year!=0)\r
- {\r
- convertedValue = new DateTime(year, mth, date);\r
- }\r
- break;\r
- case ValidationDataType.Currency:\r
- string decSep = NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator;\r
- string grpSep = NumberFormatInfo.CurrentInfo.CurrencyGroupSeparator;\r
- int decDig = NumberFormatInfo.CurrentInfo.CurrencyDecimalDigits;\r
- if(grpSep[0] == 0xA0)\r
- {\r
- grpSep = " ";\r
- }\r
- string[] patternArray = new string[5];\r
- patternArray[0] = "^\\s*([-\\+])?(((\\d+)\\";\r
- patternArray[1] = grpSep;\r
- patternArray[2] = @")*)(\d+)";\r
- if(decDig > 0)\r
- {\r
- string[] decPattern = new string[5];\r
- decPattern[0] = "(\\";\r
- decPattern[1] = decSep;\r
- decPattern[2] = @"(\d{1,";\r
- decPattern[3] = decDig.ToString(NumberFormatInfo.InvariantInfo);\r
- decPattern[4] = @"}))";\r
- patternArray[3] = String.Concat(decPattern);\r
-\r
- } else\r
- {\r
- patternArray[3] = String.Empty;\r
- }\r
- patternArray[4] = @"?\s*$";\r
- Match matchCurrency = Regex.Match(text, String.Concat(patternArray));\r
- if(matchCurrency.Success)\r
- {\r
- StringBuilder sb = new StringBuilder();\r
- sb.Append(matchCurrency.Groups[1]);\r
- CaptureCollection cc = matchCurrency.Groups[4].Captures;\r
- foreach(IEnumerable current in cc)\r
- {\r
- sb.Append((Capture)current);\r
- }\r
- sb.Append(matchCurrency.Groups[5]);\r
- if(decDig > 0)\r
- {\r
- sb.Append(".");\r
- sb.Append(matchCurrency.Groups[7]);\r
- }\r
- convertedValue = Decimal.Parse(sb.ToString(), CultureInfo.InvariantCulture);\r
- }\r
- break;\r
- }\r
- } catch(Exception e)\r
- {\r
- convertedValue = null;\r
- }\r
- return (convertedValue != null);\r
- }\r
- }\r
-}\r
+/**
+ * Namespace: System.Web.UI.WebControls
+ * Class: BaseCompareValidator
+ *
+ * Author: Gaurav Vaish
+ * Maintainer: gvaish@iitk.ac.in
+ * Implementation: yes
+ * Contact: <gvaish@iitk.ac.in>
+ * Status: 100%
+ *
+ * (C) Gaurav Vaish (2001)
+ */
+
+using System;
+using System.Collections;
+using System.Globalization;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Web;
+using System.Web.UI;
+using System.ComponentModel;
+
+namespace System.Web.UI.WebControls
+{
+ public abstract class BaseCompareValidator: BaseValidator
+ {
+ protected BaseCompareValidator(): base()
+ {
+ }
+
+ public static bool CanConvert(string text, ValidationDataType type)
+ {
+ object o = null;
+ return Convert(text, type, out o);
+ }
+
+ [DefaultValue(ValidationDataType.String)]
+ [WebCategory("Behaviour")]
+ [WebSysDescription("RangeValidator_Type")]
+ public ValidationDataType Type
+ {
+ get
+ {
+ object o = ViewState["Type"];
+ if(o!=null)
+ return (ValidationDataType)o;
+ return ValidationDataType.String;
+ }
+ set
+ {
+ if(!System.Enum.IsDefined(typeof(ValidationDataType), value))
+ throw new ArgumentException();
+ ViewState["Type"] = value;
+ }
+ }
+
+ protected static int CutoffYear
+ {
+ get
+ {
+ return DateTimeFormatInfo.CurrentInfo.Calendar.TwoDigitYearMax;
+ }
+ }
+
+ protected static int GetFullYear(int shortYear)
+ {
+ int century = DateTime.Today.Year - (DateTime.Today.Year % 100);
+ if(century < CutoffYear)
+ {
+ return (shortYear + century);
+ }
+ return (shortYear + century - 100);
+ }
+
+ protected override void AddAttributesToRender(HtmlTextWriter writer)
+ {
+ base.AddAttributesToRender(writer);
+ if(RenderUplevel)
+ {
+ writer.AddAttribute("type", PropertyConverter.EnumToString(typeof(ValidationDataType), Type));
+ NumberFormatInfo currInfo = NumberFormatInfo.CurrentInfo;
+ if(Type == ValidationDataType.Double)
+ {
+ writer.AddAttribute("decimalchar", currInfo.NumberDecimalSeparator);
+ return;
+ }
+ if(Type == ValidationDataType.Currency)
+ {
+ writer.AddAttribute("decimalchar", currInfo.CurrencyDecimalSeparator);
+ string grpSep = currInfo.CurrencyGroupSeparator;
+ if(grpSep[0] == 0xA0)
+ {
+ grpSep = " ";
+ }
+ writer.AddAttribute("groupchar", grpSep);
+ writer.AddAttribute("digits", currInfo.CurrencyDecimalDigits.ToString(NumberFormatInfo.InvariantInfo));
+ return;
+ }
+ if(Type == ValidationDataType.Date)
+ {
+ writer.AddAttribute("cutoffyear", CutoffYear.ToString());
+ writer.AddAttribute("century", ( DateTime.Today.Year - (DateTime.Today.Year % 100) ).ToString());
+ return;
+ }
+ }
+ }
+
+ protected override bool DetermineRenderUplevel()
+ {
+ if(Type == ValidationDataType.Date && DateTimeFormatInfo.CurrentInfo.Calendar.GetType() != typeof(GregorianCalendar))
+ {
+ return false;
+ }
+ return base.DetermineRenderUplevel();
+ }
+
+ /// <summary>
+ /// Undocumented
+ /// </summary>
+ protected static bool Compare(string leftText, string rightText, ValidationCompareOperator op, ValidationDataType type)
+ {
+ object left = null, right = null;
+ if(!Convert(leftText, type, out left))
+ {
+ return false;
+ }
+ if(op == ValidationCompareOperator.DataTypeCheck)
+ {
+ return true;
+ }
+ if(!Convert(rightText, type, out right))
+ {
+ return true;
+ }
+ int compareResult = 0;
+ switch(type)
+ {
+ case ValidationDataType.String:
+ compareResult = ((String)left).CompareTo(right);
+ break;
+ case ValidationDataType.Integer:
+ compareResult = ((int)left).CompareTo(right);
+ break;
+ case ValidationDataType.Double:
+ compareResult = ((Double)left).CompareTo(right);
+ break;
+ case ValidationDataType.Date:
+ compareResult = ((DateTime)left).CompareTo(right);
+ break;
+ case ValidationDataType.Currency:
+ compareResult = ((Decimal)left).CompareTo(right);
+ break;
+ }
+ switch(op)
+ {
+ case ValidationCompareOperator.Equal:
+ return (compareResult == 0);
+ case ValidationCompareOperator.NotEqual:
+ return (compareResult != 0);
+ case ValidationCompareOperator.GreaterThan:
+ return (compareResult > 0);
+ case ValidationCompareOperator.GreaterThanEqual:
+ return (compareResult >= 0);
+ case ValidationCompareOperator.LessThan:
+ return (compareResult < 0);
+ case ValidationCompareOperator.LessThanEqual:
+ return (compareResult <= 0);
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// Undocumented
+ /// </summary>
+ protected static string GetDateElementOrder()
+ {
+ string pattern = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
+
+ //TODO: What are the various possibilities?
+ // I can think of only y*/M*/d*, d*/M*/y*, M*/d*/y*
+ if(pattern.IndexOf('y') < pattern.IndexOf('M'))
+ {
+ return "ymd";
+ }
+ if(pattern.IndexOf('M') < pattern.IndexOf('d'))
+ {
+ return "mdy";
+ }
+ return "dmy";
+ }
+
+ /// <summary>
+ /// Undocumented
+ /// </summary>
+ protected static bool Convert(string text, ValidationDataType type, out object convertedValue)
+ {
+ convertedValue = null;
+ try
+ {
+ switch(type)
+ {
+ case ValidationDataType.String: convertedValue = text;
+ break;
+ case ValidationDataType.Integer: convertedValue = Int32.Parse(text, CultureInfo.InvariantCulture);
+ break;
+ case ValidationDataType.Double:
+ Match matchDouble = Regex.Match(text, @"^\s*([-\+])?(\d+)?(\"
+ + NumberFormatInfo.CurrentInfo.NumberDecimalSeparator
+ + @"(\d+))?\s*$");
+ if(matchDouble.Success)
+ {
+ string sign = (matchDouble.Groups[1].Success ? matchDouble.Groups[1].Value : "+");
+ string decPart = (matchDouble.Groups[2].Success ? matchDouble.Groups[2].Value : "0");
+ string mantissa = (matchDouble.Groups[4].Success ? matchDouble.Groups[4].Value : "0");
+ convertedValue = Double.Parse(sign + decPart + "." + mantissa, CultureInfo.InvariantCulture);
+ }
+ break;
+ case ValidationDataType.Date:
+ if(DateTimeFormatInfo.CurrentInfo.Calendar.GetType() != typeof(GregorianCalendar))
+ {
+ convertedValue = DateTime.Parse(text);
+ break;
+ }
+ string order = GetDateElementOrder();
+ int date = 0, mth = 0, year = 0;
+ Match matchDate = Regex.Match(text, @"^\s*((\d{4})|(\d{2}))([\.\/-])(\d{1,2})\4(\d{1,2})\s*$");
+ if(matchDate.Success && order == "ymd")
+ {
+ date = Int32.Parse(matchDate.Groups[6].Value, CultureInfo.InvariantCulture);
+ mth = Int32.Parse(matchDate.Groups[5].Value, CultureInfo.InvariantCulture);
+ year = Int32.Parse((matchDate.Groups[2].Success ? matchDate.Groups[2].Value : matchDate.Groups[3].Value), CultureInfo.InvariantCulture);
+ } else
+ {
+ matchDate = Regex.Match(text, @"^\s*(\d{1,2})([\.\/-])(\d{1,2})\2((\d{4}|\d{2}))\s*$");
+ if(matchDate.Success)
+ {
+ if(order == "dmy")
+ {
+ date = Int32.Parse(matchDate.Groups[1].Value, CultureInfo.InvariantCulture);
+ mth = Int32.Parse(matchDate.Groups[3].Value, CultureInfo.InvariantCulture);
+ year = Int32.Parse((matchDate.Groups[5].Success ? matchDate.Groups[5].Value : matchDate.Groups[6].Value), CultureInfo.InvariantCulture);
+ }
+ if(order == "mdy")
+ {
+ date = Int32.Parse(matchDate.Groups[3].Value, CultureInfo.InvariantCulture);
+ mth = Int32.Parse(matchDate.Groups[1].Value, CultureInfo.InvariantCulture);
+ year = Int32.Parse((matchDate.Groups[5].Success ? matchDate.Groups[5].Value : matchDate.Groups[6].Value), CultureInfo.InvariantCulture);
+ }
+ }
+ }
+ year = (year < 100 ? GetFullYear(year) : year);
+ if(matchDate.Success && date!=0 && mth!=0 && year!=0)
+ {
+ convertedValue = new DateTime(year, mth, date);
+ }
+ break;
+ case ValidationDataType.Currency:
+ string decSep = NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator;
+ string grpSep = NumberFormatInfo.CurrentInfo.CurrencyGroupSeparator;
+ int decDig = NumberFormatInfo.CurrentInfo.CurrencyDecimalDigits;
+ if(grpSep[0] == 0xA0)
+ {
+ grpSep = " ";
+ }
+ string[] patternArray = new string[5];
+ patternArray[0] = "^\\s*([-\\+])?(((\\d+)\\";
+ patternArray[1] = grpSep;
+ patternArray[2] = @")*)(\d+)";
+ if(decDig > 0)
+ {
+ string[] decPattern = new string[5];
+ decPattern[0] = "(\\";
+ decPattern[1] = decSep;
+ decPattern[2] = @"(\d{1,";
+ decPattern[3] = decDig.ToString(NumberFormatInfo.InvariantInfo);
+ decPattern[4] = @"}))";
+ patternArray[3] = String.Concat(decPattern);
+
+ } else
+ {
+ patternArray[3] = String.Empty;
+ }
+ patternArray[4] = @"?\s*$";
+ Match matchCurrency = Regex.Match(text, String.Concat(patternArray));
+ if(matchCurrency.Success)
+ {
+ StringBuilder sb = new StringBuilder();
+ sb.Append(matchCurrency.Groups[1]);
+ CaptureCollection cc = matchCurrency.Groups[4].Captures;
+ foreach(IEnumerable current in cc)
+ {
+ sb.Append((Capture)current);
+ }
+ sb.Append(matchCurrency.Groups[5]);
+ if(decDig > 0)
+ {
+ sb.Append(".");
+ sb.Append(matchCurrency.Groups[7]);
+ }
+ convertedValue = Decimal.Parse(sb.ToString(), CultureInfo.InvariantCulture);
+ }
+ break;
+ }
+ } catch(Exception e)
+ {
+ convertedValue = null;
+ }
+ return (convertedValue != null);
+ }
+ }
+}