2 // System.Web.UI.WebControls.BaseCompareValidator
5 // Chris Toshok (toshok@novell.com)
7 // (C) 2005-2010 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Threading;
31 using System.Globalization;
32 using System.ComponentModel;
33 using System.Security.Permissions;
34 using System.Web.Util;
36 namespace System.Web.UI.WebControls
39 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 public abstract class BaseCompareValidator : BaseValidator
43 protected BaseCompareValidator ()
47 protected override void AddAttributesToRender (HtmlTextWriter w)
51 RegisterExpandoAttribute (ClientID, "type", Type.ToString ());
54 case ValidationDataType.Date:
55 DateTimeFormatInfo dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
56 string pattern = dateTimeFormat.ShortDatePattern;
57 string dateorder = (pattern.StartsWith ("y", true, Helpers.InvariantCulture) ? "ymd" : (pattern.StartsWith ("m", true, Helpers.InvariantCulture) ? "mdy" : "dmy"));
58 RegisterExpandoAttribute (ClientID, "dateorder", dateorder);
59 RegisterExpandoAttribute (ClientID, "cutoffyear", dateTimeFormat.Calendar.TwoDigitYearMax.ToString ());
61 case ValidationDataType.Currency:
62 NumberFormatInfo numberFormat = CultureInfo.CurrentCulture.NumberFormat;
63 RegisterExpandoAttribute (ClientID, "decimalchar", numberFormat.CurrencyDecimalSeparator, true);
64 RegisterExpandoAttribute (ClientID, "groupchar", numberFormat.CurrencyGroupSeparator, true);
65 RegisterExpandoAttribute (ClientID, "digits", numberFormat.CurrencyDecimalDigits.ToString());
66 RegisterExpandoAttribute (ClientID, "groupsize", numberFormat.CurrencyGroupSizes [0].ToString ());
72 base.AddAttributesToRender (w);
75 public static bool CanConvert (string text,
76 ValidationDataType type)
80 return Convert (text, type, out value);
83 protected static bool Convert (string text,
84 ValidationDataType type,
87 return BaseCompareValidator.Convert(text, type, false, out value);
90 protected static bool Compare (string left, string right, ValidationCompareOperator op, ValidationDataType type)
92 return BaseCompareValidator.Compare(left, false, right, false, op, type);
95 protected override bool DetermineRenderUplevel ()
97 /* presumably the CompareValidator client side
98 * code makes use of newer dom/js stuff than
99 * the rest of the validators. but ours
100 * doesn't for the moment, so let's just use
101 * our present implementation
103 return base.DetermineRenderUplevel();
106 protected static string GetDateElementOrder ()
108 // I hope there's a better way to implement this...
109 string pattern = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
110 StringBuilder order = new StringBuilder();
111 bool seen_date = false;
112 bool seen_year = false;
113 bool seen_month = false;
115 pattern = pattern.ToLower (Helpers.InvariantCulture);
117 for (int i = 0; i < pattern.Length; i ++) {
118 char c = pattern[ i ];
119 if (c != 'm' && c != 'd' && c != 'y')
123 if (!seen_month) order.Append ("m");
125 } else if (c == 'y') {
126 if (!seen_year) order.Append ("y");
128 } else /* (c == 'd') */ {
129 if (!seen_date) order.Append ("d");
134 return order.ToString ();
137 protected static int GetFullYear (int two_digit_year)
139 /* This is an implementation that matches the
140 * docs on msdn, but MS doesn't seem to go by
141 * their docs (at least in 1.0). */
142 int cutoff = CutoffYear;
143 int twodigitcutoff = cutoff % 100;
145 if (two_digit_year <= twodigitcutoff)
146 return cutoff - twodigitcutoff + two_digit_year;
148 return cutoff - twodigitcutoff - 100 + two_digit_year;
151 [DefaultValue (false)]
153 public bool CultureInvariantValues {
154 get { return ViewState.GetBool ("CultureInvariantValues", false); }
155 set { ViewState ["CultureInvariantValues"] = value; }
158 protected static int CutoffYear {
159 get { return CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax; }
163 [DefaultValue(ValidationDataType.String)]
165 [WebSysDescription("")]
166 [WebCategory("Behavior")]
167 public ValidationDataType Type {
168 get { return ViewState ["Type"] == null ? ValidationDataType.String : (ValidationDataType) ViewState ["Type"]; }
169 set { ViewState ["Type"] = value; }
172 public static bool CanConvert (string text, ValidationDataType type, bool cultureInvariant)
175 return Convert(text, type, cultureInvariant, out value);
178 protected static bool Compare (string left,
179 bool cultureInvariantLeftText,
181 bool cultureInvariantRightText,
182 ValidationCompareOperator op,
183 ValidationDataType type)
187 if (!Convert(left, type, cultureInvariantLeftText, out lo))
190 /* DataTypeCheck is a unary operator that only
191 * depends on the lhs */
192 if (op == ValidationCompareOperator.DataTypeCheck)
195 /* pretty crackladen, but if we're unable to
196 * convert the rhs to @type, the comparison
198 if (!Convert(right, type, cultureInvariantRightText, out ro))
201 int comp = ((IComparable)lo).CompareTo((IComparable)ro);
204 case ValidationCompareOperator.Equal:
206 case ValidationCompareOperator.NotEqual:
208 case ValidationCompareOperator.LessThan:
210 case ValidationCompareOperator.LessThanEqual:
212 case ValidationCompareOperator.GreaterThan:
214 case ValidationCompareOperator.GreaterThanEqual:
221 protected static bool Convert (string text, ValidationDataType type, bool cultureInvariant,out object value)
225 case ValidationDataType.String:
227 return value != null;
229 case ValidationDataType.Integer:
230 IFormatProvider intFormatProvider = (cultureInvariant) ? NumberFormatInfo.InvariantInfo :
231 NumberFormatInfo.CurrentInfo;
232 value = Int32.Parse(text, intFormatProvider);
235 case ValidationDataType.Double:
236 IFormatProvider doubleFormatProvider = (cultureInvariant) ? NumberFormatInfo.InvariantInfo :
237 NumberFormatInfo.CurrentInfo;
238 value = Double.Parse(text, doubleFormatProvider);
241 case ValidationDataType.Date:
243 IFormatProvider dateFormatProvider = (cultureInvariant) ? DateTimeFormatInfo.InvariantInfo :
244 DateTimeFormatInfo.CurrentInfo;
246 value = DateTime.Parse(text, dateFormatProvider);
249 case ValidationDataType.Currency:
250 IFormatProvider currencyFormatProvider = (cultureInvariant) ? NumberFormatInfo.InvariantInfo :
251 NumberFormatInfo.CurrentInfo;
252 value = Decimal.Parse(text, NumberStyles.Currency, currencyFormatProvider);