private static void TrySetObservable(Func <object> valueAccessor, jQueryObject inputField, string value) { Observable <int?> observable = (Observable <int?>)valueAccessor(); bool isValid = true; bool isEmpty = (value == null) || (value.Length == 0); // Get the value as minutes decimal // ([0-9]*) ((h(our)?[s]?)|(m(inute)?[s]?)|(d(ay)?[s]?)) string pattern = @"/([0-9]*)[ ]?((h(our)?[s]?)|(m(inute)?[s]?)|(d(ay)?[s]?))/g"; RegularExpression regex = RegularExpression.Parse(pattern); string[] match = regex.Exec(value); if (isEmpty) { observable.SetValue(null); } else if (match != null && match.Length > 0) { // Get value decimal durationNumber = decimal.Parse(match[1]); switch (match[2].Substr(0, 1).ToLowerCase()) { case "d": durationNumber = durationNumber * 60 * 24; break; case "h": durationNumber = durationNumber * 60; break; } observable.SetValue((int?)durationNumber); if (((string)Script.Literal("typeof({0}.isValid)", observable)) != "undefined") { isValid = ((IValidatedObservable)observable).IsValid() == true; } if (isValid) { //inputField.Blur(); } } else { Script.Alert("Invalid Duration Format"); int? currentValue = observable.GetValue(); string durationString = formatDuration(currentValue); inputField.Value(durationString); inputField.Focus(); } }
public object Validate(string value, ValidationContext context) { if (ValidationUtil.StringIsNullOrEmpty(value)) { return(true); // let the RequiredValidator handle this case } RegularExpression regExp = new RegularExpression(_pattern); string[] matches = regExp.Exec(value); return(!ValidationUtil.ArrayIsNullOrEmpty(matches) && matches[0].Length == value.Length); }
public static int?ParseDuration(string duration) { bool isEmpty = (duration == null) || (duration.Length == 0); if (isEmpty) { return(null); } // Get the value as minutes decimal // ([0-9]*) ((h(our)?[s]?)|(m(inute)?[s]?)|(d(ay)?[s]?)) string pattern = @"/([0-9.]*)[ ]?((h(our)?[s]?)|(m(inute)?[s]?)|(d(ay)?[s]?))/g"; RegularExpression regex = RegularExpression.Parse(pattern); bool matched = false; bool thisMatch = false; int totalDuration = 0; do { string[] match = regex.Exec(duration); thisMatch = (match != null && match.Length > 0); matched = matched || thisMatch; if (thisMatch) { // Get value decimal durationNumber = decimal.Parse(match[1]); switch (match[2].Substr(0, 1).ToLowerCase()) { case "d": durationNumber = durationNumber * 60 * 24; break; case "h": durationNumber = durationNumber * 60; break; } totalDuration += Math.Floor(durationNumber); // Remove matched item duration.Replace(match[0], ""); } } while (thisMatch); if (matched) { return((int?)totalDuration); } else { return(null); } }