コード例 #1
0
ファイル: MyEntry.cs プロジェクト: xhackers/xamarin-amccorma
        public string ReFractor(string text, MaskRules rule)
        {
            string temp = "";

            if (rule.Mask != "")
            {
                var result = System.Text.RegularExpressions.Regex.Match(rule.Mask, CV_defaultMask);
                temp = rule.Mask;
                do
                {
                    if (System.Text.RegularExpressions.Regex.Match(result.Value, CV_defaultOneMask).Success)
                    {
                        // end match
                        var obj = ConvertMatch(result.Value);
                        temp = temp.Replace(result.Value, text.Substring(obj[0]));
                        break;
                    }
                    else
                    {
                        var obj = ConvertMatch(result.Value);
                        temp   = temp.Replace(result.Value, text.Substring(obj[0], obj[1]));
                        result = result.NextMatch();
                    }
                } while (true);
                return(temp);
            }
            return(text);
        }
コード例 #2
0
        public string ApplyMask(string text, MaskRules rule)
        {
            string temp = "";

            if (rule.Mask != "")
            {
                // adjust length of greater than last mask entry
                if (text.Length > this.MaxLengthFromMask)
                {
                    text = text.Substring(0, this.MaxLengthFromMask);
                }

                var result = System.Text.RegularExpressions.Regex.Match(rule.Mask, CV_defaultMask);
                temp = rule.Mask;
                do
                {
                    if (System.Text.RegularExpressions.Regex.Match(result.Value, CV_defaultOneMask).Success || String.IsNullOrEmpty(result.Value))
                    {
                        // result is an empty string. return. done parsing
                        if (String.IsNullOrEmpty(result.Value))
                        {
                            break;
                        }

                        // end match
                        var obj = ConvertMatch(result.Value);
                        temp = temp.Replace(result.Value, text.Substring(obj[0]));
                        break;
                    }
                    else
                    {
                        var obj = ConvertMatch(result.Value);
                        if (obj[0] + obj[1] >= text.Length)
                        {
                            // keeping format character at end??
                            var t = result.Value[result.Value.Length - 1];
                            temp = temp.Replace(result.Value, text.Substring(obj[0]));
                        }
                        else
                        {
                            temp = temp.Replace(result.Value, text.Substring(obj[0], obj[1]));
                        }
                        result = result.NextMatch();
                    }
                } while (true);
                return(temp);
            }
            return(text);
        }
コード例 #3
0
		public string ApplyMask(string text, MaskRules rule)
		{
			string temp = "";
			if (rule.Mask != "")
			{
				// adjust length of greater than last mask entry
				if (text.Length > this.MaxLengthFromMask)
				{
					text = text.Substring (0, this.MaxLengthFromMask);
				}

				var result = System.Text.RegularExpressions.Regex.Match(rule.Mask, CV_defaultMask);
				temp = rule.Mask;
				do
				{
					if (System.Text.RegularExpressions.Regex.Match(result.Value, CV_defaultOneMask).Success || String.IsNullOrEmpty(result.Value))
					{
						// result is an empty string. return. done parsing
						if (String.IsNullOrEmpty(result.Value)) break;

						// end match                            
						var obj = ConvertMatch(result.Value);
						temp = temp.Replace(result.Value, text.Substring(obj[0]));
						break;
					}
					else
					{						
						var obj = ConvertMatch(result.Value);
						if (obj[0] + obj[1] >= text.Length)
						{
							// keeping format character at end??
							var t = result.Value[result.Value.Length-1];
							temp = temp.Replace(result.Value, text.Substring(obj[0])); 
						}
						else
						{
							temp = temp.Replace(result.Value, text.Substring(obj[0], obj[1]));
						}
						result = result.NextMatch();
					}
				} while (true);
				return temp;
			}
			return text;
		}
コード例 #4
0
        public bool Validate(string text, MaskRules rule)
        {
            bool v = true;

            if (String.IsNullOrEmpty(text))
            {
                return(true);
            }

            var validCharacters = ValidationRules.FirstOrDefault(x => x.Operation == Validators.ONLYCHARS);

            if (ValidationRules != null)
            {
                v = System.Text.RegularExpressions.Regex.Match(text, validCharacters.Arg).Success;
                ValidationError(validCharacters.ErrorMessage);
            }

            if (v)
            {
                var max = ValidationRules.FirstOrDefault(x => x.Operation == Validators.MAX);
                if (max != null && text.Length > max.ArgAsInt)
                {
                    v = false;
                    ValidationError(max.ErrorMessage);
                }
            }

            if (v && rule != null && rule.Rules != null)
            {
                var startC = rule.Rules.FirstOrDefault(x => x.Operation == Validators.STARTC);
                if (startC != null)
                {
                    if (startC.Arg.Length > text.Length - rule.Start)
                    {
                        v = String.Compare(text.Substring(rule.Start), startC.Arg.Substring(0, text.Length - rule.Start),
                                           (startC.CaseCheck == false ? StringComparison.OrdinalIgnoreCase : StringComparison.CurrentCulture)) == 0;
                        ValidationError(startC.ErrorMessage);
                    }
                    else if (startC.Arg.Length > text.Length - rule.Start)
                    {
                        v = String.Compare(text.Substring(rule.Start, startC.Arg.Length), startC.Arg,
                                           (startC.CaseCheck == false ? StringComparison.OrdinalIgnoreCase : StringComparison.CurrentCulture)) == 0;
                        ValidationError(startC.ErrorMessage);
                    }
                }

                var endC = rule.Rules.FirstOrDefault(x => x.Operation == Validators.ENDC);
                if (endC != null)
                {
                    var d = rule.End - endC.Arg.Length;
                    if (text.Length + endC.Arg.Length > rule.End)
                    {
                        var test = text.Substring(d);
                        v = String.Compare(test, endC.Arg.Substring(0, test.Length),
                                           (endC.CaseCheck == false ? StringComparison.OrdinalIgnoreCase : StringComparison.CurrentCulture)) == 0;
                        ValidationError(endC.ErrorMessage);
                    }
                }
            }

            return(v);
        }