2 // System.Web.UI.WebControls.BaseCompareValidator
5 // Chris Toshok (toshok@novell.com)
7 // (C) 2005 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;
35 namespace System.Web.UI.WebControls {
38 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 public abstract class BaseCompareValidator : BaseValidator {
47 BaseCompareValidator ()
51 protected override void AddAttributesToRender (HtmlTextWriter w)
56 RegisterExpandoAttribute (ClientID, "type", Type.ToString ());
59 case ValidationDataType.Date:
60 DateTimeFormatInfo dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
61 string pattern = dateTimeFormat.ShortDatePattern;
62 string dateorder = (pattern.StartsWith ("y", true, CultureInfo.InvariantCulture) ? "ymd" : (pattern.StartsWith ("m", true, CultureInfo.InvariantCulture) ? "mdy" : "dmy"));
63 RegisterExpandoAttribute (ClientID, "dateorder", dateorder);
64 RegisterExpandoAttribute (ClientID, "cutoffyear", dateTimeFormat.Calendar.TwoDigitYearMax.ToString ());
66 case ValidationDataType.Currency:
67 NumberFormatInfo numberFormat = CultureInfo.CurrentCulture.NumberFormat;
68 RegisterExpandoAttribute (ClientID, "decimalchar", numberFormat.CurrencyDecimalSeparator, true);
69 RegisterExpandoAttribute (ClientID, "groupchar", numberFormat.CurrencyGroupSeparator, true);
70 RegisterExpandoAttribute (ClientID, "digits", numberFormat.CurrencyDecimalDigits.ToString());
71 RegisterExpandoAttribute (ClientID, "groupsize", numberFormat.CurrencyGroupSizes [0].ToString ());
76 w.AddAttribute ("datatype", Type.ToString());
80 base.AddAttributesToRender (w);
83 public static bool CanConvert (string text,
84 ValidationDataType type)
88 return Convert (text, type, out value);
91 protected static bool Convert (string text,
92 ValidationDataType type,
95 return BaseCompareValidator.Convert(text, type, false, out value);
98 protected static bool Compare (string left,
100 ValidationCompareOperator op,
101 ValidationDataType type)
103 return BaseCompareValidator.Compare(left, false, right, false, op, type);
106 protected override bool DetermineRenderUplevel ()
108 /* presumably the CompareValidator client side
109 * code makes use of newer dom/js stuff than
110 * the rest of the validators. but ours
111 * doesn't for the moment, so let's just use
112 * our present implementation
114 return base.DetermineRenderUplevel();
117 protected static string GetDateElementOrder ()
119 // I hope there's a better way to implement this...
120 string pattern = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
121 StringBuilder order = new StringBuilder();
122 bool seen_date = false;
123 bool seen_year = false;
124 bool seen_month = false;
126 pattern = pattern.ToLower (CultureInfo.InvariantCulture);
128 for (int i = 0; i < pattern.Length; i ++) {
129 char c = pattern[ i ];
130 if (c != 'm' && c != 'd' && c != 'y')
134 if (!seen_month) order.Append ("m");
138 if (!seen_year) order.Append ("y");
141 else /* (c == 'd') */ {
142 if (!seen_date) order.Append ("d");
147 return order.ToString ();
150 protected static int GetFullYear (int two_digit_year)
153 /* This is an implementation that matches the
154 * docs on msdn, but MS doesn't seem to go by
155 * their docs (at least in 1.0). */
156 int cutoff = CutoffYear;
157 int twodigitcutoff = cutoff % 100;
159 if (two_digit_year <= twodigitcutoff) {
160 return cutoff - twodigitcutoff + two_digit_year;
163 return cutoff - twodigitcutoff - 100 + two_digit_year;
166 /* This is the broken implementation in 1.0 */
167 int cutoff = CutoffYear;
168 int twodigitcutoff = cutoff % 100;
170 return cutoff - twodigitcutoff + two_digit_year;
175 [DefaultValue (false)]
181 bool CultureInvariantValues {
182 get { return ViewState.GetBool ("CultureInvariantValues", false); }
183 set { ViewState ["CultureInvariantValues"] = value; }
188 protected static int CutoffYear {
190 return CultureInfo.CurrentCulture.Calendar.TwoDigitYearMax;
195 [DefaultValue(ValidationDataType.String)]
199 [WebSysDescription("")]
200 [WebCategory("Behavior")]
201 public ValidationDataType Type {
202 get { return ViewState ["Type"] == null ? ValidationDataType.String : (ValidationDataType) ViewState ["Type"]; }
203 set { ViewState ["Type"] = value; }
212 static bool CanConvert (string text,
213 ValidationDataType type,
214 bool cultureInvariant)
217 return Convert(text, type, cultureInvariant, out value);
225 static bool Compare (string left,
226 bool cultureInvariantLeftText,
228 bool cultureInvariantRightText,
229 ValidationCompareOperator op,
230 ValidationDataType type)
234 if (!Convert(left, type, cultureInvariantLeftText, out lo))
237 /* DataTypeCheck is a unary operator that only
238 * depends on the lhs */
239 if (op == ValidationCompareOperator.DataTypeCheck)
242 /* pretty crackladen, but if we're unable to
243 * convert the rhs to @type, the comparison
245 if (!Convert(right, type, cultureInvariantRightText, out ro))
248 int comp = ((IComparable)lo).CompareTo((IComparable)ro);
252 case ValidationCompareOperator.Equal:
254 case ValidationCompareOperator.NotEqual:
256 case ValidationCompareOperator.LessThan:
258 case ValidationCompareOperator.LessThanEqual:
260 case ValidationCompareOperator.GreaterThan:
262 case ValidationCompareOperator.GreaterThanEqual:
274 static bool Convert (string text,
275 ValidationDataType type,
276 bool cultureInvariant,
283 case ValidationDataType.String:
285 return value != null;
287 case ValidationDataType.Integer:
288 IFormatProvider intFormatProvider = (cultureInvariant) ?
289 NumberFormatInfo.InvariantInfo :
290 NumberFormatInfo.CurrentInfo;
291 value = Int32.Parse(text, intFormatProvider);
294 case ValidationDataType.Double:
295 IFormatProvider doubleFormatProvider = (cultureInvariant) ?
296 NumberFormatInfo.InvariantInfo :
297 NumberFormatInfo.CurrentInfo;
298 value = Double.Parse(text, doubleFormatProvider);
301 case ValidationDataType.Date:
303 IFormatProvider dateFormatProvider = (cultureInvariant) ?
304 DateTimeFormatInfo.InvariantInfo :
305 DateTimeFormatInfo.CurrentInfo;
307 value = DateTime.Parse(text, dateFormatProvider);
310 case ValidationDataType.Currency:
311 IFormatProvider currencyFormatProvider = (cultureInvariant) ?
312 NumberFormatInfo.InvariantInfo :
313 NumberFormatInfo.CurrentInfo;
314 value = Decimal.Parse(text, NumberStyles.Currency, currencyFormatProvider);