1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Components
.Validator
18 using System
.Collections
;
19 using System
.Text
.RegularExpressions
;
22 /// Validate a property using regular expression
25 public class RegularExpressionValidator
: AbstractValidator
27 private readonly Regex regexRule
;
28 private readonly string expression
;
31 /// Initializes a new instance of the <see cref="RegularExpressionValidator"/> class.
33 /// <param name="expression">The expression.</param>
34 public RegularExpressionValidator(String expression
) : this(expression
, RegexOptions
.Compiled
)
39 /// Initializes a new instance of the <see cref="RegularExpressionValidator"/> class.
41 /// <param name="expression">The expression.</param>
42 /// <param name="options">The regular expression options.</param>
43 public RegularExpressionValidator(String expression
, RegexOptions options
)
45 this.expression
= expression
;
46 regexRule
= new Regex(expression
, options
);
51 /// Gets the regular expression object.
53 /// <value>The regular expression object.</value>
54 public Regex RegexRule
56 get { return regexRule; }
60 /// Gets the expression.
62 /// <value>The expression.</value>
63 public string Expression
65 get { return expression; }
69 /// Validate that the property value match the given regex. Null or empty values are allowed.
71 /// <param name="instance"></param>
72 /// <param name="fieldValue"></param>
73 /// <returns><c>true</c> if the field is OK</returns>
74 public override bool IsValid(object instance
, object fieldValue
)
76 if (fieldValue
!= null && fieldValue
.ToString() != "")
78 string value = fieldValue
.ToString();
82 return regexRule
.IsMatch(value);
90 /// Gets a value indicating whether this validator supports browser validation.
93 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
95 public override bool SupportsBrowserValidation
101 /// Applies the browser validation by setting up one or
102 /// more input rules on <see cref="IBrowserValidationGenerator"/>.
104 /// <param name="config">The config.</param>
105 /// <param name="inputType">Type of the input.</param>
106 /// <param name="generator">The generator.</param>
107 /// <param name="attributes">The attributes.</param>
108 /// <param name="target">The target.</param>
109 public override void ApplyBrowserValidation(BrowserValidationConfiguration config
, InputElementType inputType
,
110 IBrowserValidationGenerator generator
, IDictionary attributes
,
113 base.ApplyBrowserValidation(config
, inputType
, generator
, attributes
, target
);
115 generator
.SetRegExp(target
, expression
, BuildErrorMessage());