public static bool TryParse(string text, ParseAttrib param, out int value)
 {
     if (string.IsNullOrEmpty(text))
     {
         value = 0; return(false);
     }
     if (!int.TryParse(text, out value))
     {
         return(false);
     }
     // check param
     if (param != null)
     {
         int?minValue = param.MinValue;
         if ((minValue != null) && (value < minValue))
         {
             return(false);
         }
         int?maxValue = param.MaxValue;
         if ((maxValue != null) && (value > maxValue))
         {
             return(false);
         }
     }
     return(true);
 }
示例#2
0
 public static bool TryParse(string text, ParseAttrib param, out string value)
 {
     if (string.IsNullOrEmpty(text))
     {
         value = string.Empty; return(false);
     }
     value = text;
     // check param
     if (param != null)
     {
         int?minLength = param.MinLength;
         if ((minLength != null) && (value.Length < minLength))
         {
             return(false);
         }
         int?maxLength = param.MaxLength;
         if ((maxLength != null) && (value.Length > maxLength))
         {
             return(false);
         }
         int?maxNonWhiteSpaceLength = param.MaxNonWhiteSpaceLength;
         if ((maxNonWhiteSpaceLength != null) && (Regex.Replace(value, @"\s", string.Empty, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline).Length > maxNonWhiteSpaceLength))
         {
             return(false);
         }
         int?maxLines = param.MaxLines;
         if ((maxLines != null) && (value.Split(new string[] { "\r\n" }, StringSplitOptions.None).Length > maxLines))
         {
             return(false);
         }
     }
     return(true);
 }
 public static bool TryParse(string text, ParseAttrib param, out string value)
 {
     if (string.IsNullOrEmpty(text))
     {
         value = string.Empty; return(false);
     }
     value = text;
     // check param
     if (param != null)
     {
         string pattern = param.Pattern;
         if ((pattern != null) && (!Regex.IsMatch(value, pattern)))
         {
             return(false);
         }
         int?minLength = param.MinLength;
         if ((minLength != null) && (value.Length < minLength))
         {
             return(false);
         }
         int?maxLength = param.MaxLength;
         if ((maxLength != null) && (value.Length > maxLength))
         {
             return(false);
         }
     }
     return(true);
 }
 public static bool TryParse(string text, ParseAttrib param, out DateTime value)
 {
     if (string.IsNullOrEmpty(text))
     {
         value = DateTime.MinValue; return(false);
     }
     if (!DateTime.TryParse(text, out value))
     {
         return(false);
     }
     else if ((value < _minValue) || (value > _maxValue))
     {
         return(false);
     }
     // check param
     if (param != null)
     {
         DateTime?minValue = param.MinValue;
         if ((minValue != null) && (value < minValue))
         {
             return(false);
         }
         DateTime?maxValue = param.MaxValue;
         if ((maxValue != null) && (value > maxValue))
         {
             return(false);
         }
     }
     return(true);
 }
 public static bool TryParse(string text, ParseAttrib param, out string value)
 {
     if (TextConverter.Prime.TryParse(text, param, out value))
     {
         return(Regex.IsMatch(text, UriIDPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline));
     }
     return(false);
 }
 public static bool TryParse(string text, ParseAttrib param, out int value)
 {
     if (string.IsNullOrEmpty(text))
     {
         value = 0; return(false);
     }
     return(int.TryParse(text, out value));
 }
            public static bool TryParse(string text, ParseAttrib param, out ZipStruct value)
            {
                if (string.IsNullOrEmpty(text))
                {
                    value = EmptyZipStruct; return(false);
                }
                // check param
                Func <CountryID> countryIdDelegate;
                var countryId = ((param != null) && ((countryIdDelegate = param.CountryID) != null) ? countryIdDelegate() : CountryID.Usa);
                int textLength;

                if ((countryId & CountryID.Canada) == CountryID.Canada)
                {
                    // canada/generic parsing
                    text       = StringEx.ExtractString.ExtractAlphaDigit(text);
                    textLength = text.Length;
                    if ((textLength == 6) &&
                        (char.IsLetter(text[0])) && (char.IsDigit(text[1])) && (char.IsLetter(text[2])) &&
                        (char.IsDigit(text[3])) && (char.IsLetter(text[4])) && (char.IsDigit(text[5])))
                    {
                        var zip  = text.Substring(0, 3);
                        var zip2 = text.Substring(3);
                        value = new CanadaZipStruct {
                            Text = zip + " " + zip2, Zip = zip, Zip2 = zip2
                        }; return(true);
                    }
                }
                if ((countryId & CountryID.Usa) == CountryID.Usa)
                {
                    // usa/generic parsing
                    text       = StringEx.ExtractString.ExtractDigit(text);
                    textLength = text.Length;
                    if ((textLength >= 7) && (textLength <= 9))
                    {
                        var zip5 = text.Substring(0, 5);
                        var zip4 = text.Substring(5).PadLeft(4, '0');
                        value = new UsaZipStruct {
                            Text = zip5 + "-" + zip4, Zip5 = zip5, Zip4 = zip4
                        }; return(true);
                    }
                    else if ((textLength >= 3) && (textLength <= 5))
                    {
                        var zip5 = text.PadLeft(5, '0');
                        value = new UsaZipStruct {
                            Text = zip5, Zip5 = zip5
                        }; return(true);
                    }
                }
                if ((countryId & CountryID.None) == CountryID.None)
                {
                    // accept all
                    value = new ZipStruct {
                        Text = text
                    }; return(true);
                }
                value = EmptyZipStruct; return(false);
            }
 public static bool TryParse(string text, ParseAttrib param, out decimal value)
 {
     if (string.IsNullOrEmpty(text))
     {
         value = 0M; return(false);
     }
     if (text.EndsWith("%"))
     {
         text = text.Substring(0, text.Length - 1);
     }
     return(decimal.TryParse(text, out value));
 }
示例#9
0
 public static bool TryParse(string text, ParseAttrib param, out string value)
 {
     if (string.IsNullOrEmpty(text))
     {
         value = string.Empty; return(false);
     }
     // static has cached version
     if (!Regex.IsMatch(text, EmailPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline))
     {
         value = string.Empty; return(false);
     }
     value = text; return(true);
 }
示例#10
0
            public static bool TryParse(string text, ParseAttrib param, out string value)
            {
                if (string.IsNullOrEmpty(text))
                {
                    value = string.Empty; return(false);
                }
                ICollection <ParseFormat.Range <int> > ranges;

                if (!ParserEx.TryParseRanges(text, RangeParser, param, out ranges))
                {
                    value = string.Empty; return(false);
                }
                value = text; return(true);
            }
            public static bool TryParse(string text, ParseAttrib param, out string value)
            {
                if (string.IsNullOrEmpty(text))
                {
                    value = string.Empty; return(false);
                }
                // pre-check param
                UriKind uriKind = ((param == null) || (param.UriKind == null) ? UriKind.RelativeOrAbsolute : param.UriKind.Value);

                if ((Uri.IsWellFormedUriString(text, uriKind)) ||
                    ((uriKind != UriKind.Absolute) && (Uri.IsWellFormedUriString((text.Contains("://") ? text : "http://domain.com" + text), UriKind.RelativeOrAbsolute))))
                {
                    value = text; return(true);
                }
                value = string.Empty; return(false);
            }
            public static bool TryParse(string text, ParseAttrib param, out DateTime value)
            {
                if (string.IsNullOrEmpty(text))
                {
                    value = DateTime.MinValue; return(false);
                }
                // static has cached version
                var match = Regex.Match(text, @"^\d{1,2}([/\-])\d{1,2}$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);

                if ((!match.Success) || (match.Groups.Count != 2))
                {
                    value = DateTime.MinValue;
                    return(false);
                }
                // fix year
                text += match.Groups[1].Value + "1904";
                return(DateTime.TryParse(text, out value));
            }
            public static bool TryParse(string text, ParseAttrib param, out string value)
            {
                if (string.IsNullOrEmpty(text))
                {
                    value = string.Empty; return(false);
                }
                var valueStream = new StringBuilder();

                string[] hostnameArray   = text.Replace("\r", "").Replace('\n', ';').Replace(',', ';').Split(';');
                int      hostnameCount   = 0;
                string   separator       = ((param == null) || (param.Separator == null) ? "\n" : param.Separator);
                int      separatorLength = separator.Length;

                foreach (string hostname in hostnameArray)
                {
                    string hostname2 = hostname.Trim();
                    if (hostname2.Length == 0)
                    {
                        continue;
                    }
                    // static has cached version
                    if (!Regex.IsMatch(text, HostnameConverter.HostnamePattern, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline))
                    {
                        value = string.Empty; return(false);
                    }
                    hostnameCount++;
                    valueStream.Append(hostname2 + separator);
                }
                if (valueStream.Length > 1)
                {
                    valueStream.Length -= separatorLength;
                }
                value = valueStream.ToString();
                // check param
                if (param != null)
                {
                    int?maxCount = param.MaxCount;
                    if ((maxCount != null) && (hostnameCount > maxCount))
                    {
                        return(false);
                    }
                }
                return(true);
            }
示例#14
0
            public static bool TryParse(string text, ParseAttrib param, out string value)
            {
                if (string.IsNullOrEmpty(text))
                {
                    value = string.Empty; return(false);
                }
                // check param
                var settings = new XmlReaderSettings();

                if (param != null)
                {
                    if (param.ConformanceLevel != null)
                    {
                        settings.ConformanceLevel = param.ConformanceLevel.Value;
                    }
                    if (param.ValidationType != null)
                    {
                        settings.ValidationType = param.ValidationType.Value;
                        switch (settings.ValidationType)
                        {
                        case ValidationType.DTD:
                            settings.ProhibitDtd = false;
                            break;

                        case ValidationType.Schema:
                            string targetNamespace = param.TargetNamespace;
                            string schemaUri       = param.SchemaUri;
                            if ((!string.IsNullOrEmpty(targetNamespace)) && (!string.IsNullOrEmpty(schemaUri)))
                            {
                                settings.Schemas.Add(targetNamespace, schemaUri);
                            }
                            break;
                        }
                    }
                }
                try
                {
                    var xmlReader = XmlReader.Create(new StringReader(text), settings);
                    xmlReader.Close();
                }
                catch { value = string.Empty; return(false); }
                value = text; return(true);
            }
示例#15
0
            public static bool TryParse(string text, ParseAttrib param, out string value)
            {
                if (string.IsNullOrEmpty(text))
                {
                    value = string.Empty; return(false);
                }
                var valueStream = new StringBuilder();

                string[] emails     = text.Replace(',', ';').Split(';');
                int      emailCount = 0;

                foreach (string email in emails)
                {
                    string email2 = email.Trim();
                    if (email2.Length == 0)
                    {
                        continue;
                    }
                    // static has cached version
                    if (!Regex.IsMatch(email2, EmailConverter.EmailPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline))
                    {
                        value = string.Empty; return(false);
                    }
                    emailCount++;
                    valueStream.Append(email2 + "; ");
                }
                if (valueStream.Length > 1)
                {
                    valueStream.Length -= 2;
                }
                value = valueStream.ToString();
                // check param
                if (param != null)
                {
                    int?maxCount = param.MaxCount;
                    if ((maxCount != null) && (emailCount > maxCount))
                    {
                        return(false);
                    }
                }
                return(true);
            }
示例#16
0
            public static bool TryParse(string text, ParseAttrib param, out string value)
            {
                if (string.IsNullOrEmpty(text))
                {
                    value = string.Empty;
                    return(false);
                }
                // check param
                Func <CountryID> countryIdDelegate;
                var countryId = ((param != null) && ((countryIdDelegate = param.CountryID) != null) ? countryIdDelegate() : CountryID.Usa);
                // globalized processing
                int textLength;

                if (((countryId & CountryID.Canada) == CountryID.Canada) || ((countryId & CountryID.Usa) == CountryID.Usa))
                {
                    // canada+usa/generic parsing
                    text       = StringEx.ExtractString.ExtractDigit(text);
                    textLength = text.Length;
                    if (textLength > 10)
                    {
                        value = text.Substring(0, 3) + '-' + text.Substring(3, 3) + '-' + text.Substring(6, 4) + " x" + text.Substring(10); return(true);
                    }
                    else if (textLength == 10)
                    {
                        value = text.Substring(0, 3) + '-' + text.Substring(3, 3) + '-' + text.Substring(6, 4); return(true);
                    }
                    else if (textLength == 7)
                    {
                        value = text.Substring(0, 3) + '-' + text.Substring(3, 4); return(true);
                    }
                }
                if ((countryId & CountryID.None) == CountryID.None)
                {
                    // accept all
                    value = text; return(true);
                }
                value = string.Empty; return(false);
            }
示例#17
0
 public static bool TryParse(string text, ParseAttrib param, out decimal value)
 {
     if (string.IsNullOrEmpty(text))
     {
         value = 0M; return(false);
     }
     if (!decimal.TryParse(text, NumberStyles.Currency, null, out value))
     {
         return(false);
     }
     value = Math.Round(value, 4);
     // check param
     if (param != null)
     {
         decimal?minValue = param.MinValue;
         if ((minValue != null) && (value < minValue))
         {
             return(false);
         }
         decimal?maxValue = param.MaxValue;
         if ((maxValue != null) && (value > maxValue))
         {
             return(false);
         }
         int?precision = param.Precision;
         if ((precision != null) && (value != Math.Round(value, (int)precision)))
         {
             return(false);
         }
         int?round = param.Round;
         if (round != null)
         {
             value = Math.Round(value, (int)round);
         }
     }
     return(true);
 }
示例#18
0
 public static bool TryParse(string text, ParseAttrib param, out DateTime value)
 {
     if (string.IsNullOrEmpty(text))
     {
         value = MinValue; return(false);
     }
     if (!DateTime.TryParse(text, null, DateTimeStyles.NoCurrentDateDefault, out value))
     {
         return(false);
     }
     value = new DateTime(1900, 1, 1, value.Hour, value.Minute, value.Second);
     // check param
     if (param != null)
     {
         DateTime?minValue = param.MinValue;
         if (minValue != null)
         {
             var validMinValue = minValue.Value;
             validMinValue = new DateTime(1900, 1, 1, validMinValue.Hour, validMinValue.Minute, validMinValue.Second);
             if (value < validMinValue)
             {
                 return(false);
             }
         }
         DateTime?maxValue = param.MaxValue;
         if (maxValue != null)
         {
             var validMaxValue = maxValue.Value;
             validMaxValue = new DateTime(1900, 1, 1, validMaxValue.Hour, validMaxValue.Minute, validMaxValue.Second);
             if (value > validMaxValue)
             {
                 return(false);
             }
         }
     }
     return(true);
 }
示例#19
0
 public static bool TryParse(string text, ParseAttrib param, out DateTime value)
 {
     if (string.IsNullOrEmpty(text))
     {
         value = DateTime.MinValue; return(false);
     }
     if (!DateTime.TryParse(text, out value))
     {
         return(false);
     }
     else if ((value < _minValue) || (value > _maxValue))
     {
         return(false);
     }
     value = new DateTime(value.Year, value.Month, value.Day, 0, 0, 0);
     // check param
     if (param != null)
     {
         DateTime?minValue = param.MinValue;
         if ((minValue != null) && (value < minValue))
         {
             return(false);
         }
         DateTime?maxValue = param.MaxValue;
         if ((maxValue != null) && (value > maxValue))
         {
             return(false);
         }
         // roll date for creditcard
         bool?endOfMonth = param.EndOfMonth;
         if ((endOfMonth != null) && (endOfMonth == true))
         {
             value = value.ShiftDate(DateTimeEx.ShiftDateMethod.EndOfMonth);
         }
     }
     return(true);
 }
            public static bool TryParse(string text, ParseAttrib param, out bool value)
            {
                if (string.IsNullOrEmpty(text))
                {
                    value = false; return(false);
                }
                switch (text.ToLowerInvariant())
                {
                case "1":
                case "y":
                case "true":
                case "yes":
                case "on":
                    value = true; return(true);

                case "0":
                case "n":
                case "false":
                case "no":
                case "off":
                    value = false; return(true);
                }
                return(bool.TryParse(text, out value));
            }
示例#21
0
 public static bool TryParse(string text, ParseAttrib param, out double value)
 {
     if (string.IsNullOrEmpty(text))
     {
         value = 0D; return(false);
     }
     if (!double.TryParse(text, out value))
     {
         return(false);
     }
     // check param
     if (param != null)
     {
         double?minValue = param.MinValue;
         if ((minValue != null) && (value < minValue))
         {
             return(false);
         }
         double?maxValue = param.MaxValue;
         if ((maxValue != null) && (value > maxValue))
         {
             return(false);
         }
         int?precision = param.Precision;
         if ((precision != null) && (value != Math.Round(value, (int)precision)))
         {
             return(false);
         }
         int?round = param.Round;
         if (round != null)
         {
             value = Math.Round(value, (int)round);
         }
     }
     return(true);
 }
示例#22
0
 public static bool TryParse(string text, ParseAttrib param, out string value)
 {
     // make sure has at least one number
     return(TextConverter.Prime.TryParse(text, param, out value) ? (StringEx.ExtractString.ExtractDigit(value).Length > 0) : false);
 }