diff --git a/doc/reference/src/validation.xml b/doc/reference/src/validation.xml index 61dad4d2..c636b4ea 100644 --- a/doc/reference/src/validation.xml +++ b/doc/reference/src/validation.xml @@ -426,16 +426,14 @@ The syntax is - <v:regex id="id" test="valueToEvaluate" when="applicabilityCondition" parent="parentValidator"> - <v:property name="Expression" value="regularExpressionToMatch"/> + <v:regex id="id" test="valueToEvaluate" expression="regularExpressionToMatch" when="applicabilityCondition" parent="parentValidator"> <v:property name="Options" value="regexOptions"/> actions </v:regex> An example is shown below - <v:regex test="ReturningFrom.AirportCode"> - <v:property name="Expression" value="[A-Z][A-Z][A-Z]"/> + <v:regex test="ReturningFrom.AirportCode" expression="[A-Z][A-Z][A-Z]"> <v:message id="error.destinationAirport.threeCharacters" providers="destinationAirportErrors, validationSummary"/> </v:regex> @@ -443,10 +441,18 @@ that need to conform to some predefined format, such as telephone numbers, email addresses, URLs, etc. - One major difference of the regular expression validator compared - to other built-in validator types is that you need to set a required - Expression property to a regular expression to match - against. + + + Note that current behavior limits the Regular Expression Validator to expressions + to being full matches, i.e., ^(expression)$, thus limiting functionality. + To not change this behavior in a point release, a property AllowPartialMatching + has been added in 1.3.1 to support the correct behavior. + + + This property will be removed for next major/minor version 2.0 and implementation + will be fixed to get the intented behavior. + +
diff --git a/src/Spring/Spring.Core/Validation/Validators/RegularExpressionValidator.cs b/src/Spring/Spring.Core/Validation/Validators/RegularExpressionValidator.cs index 73920392..924d5c76 100644 --- a/src/Spring/Spring.Core/Validation/Validators/RegularExpressionValidator.cs +++ b/src/Spring/Spring.Core/Validation/Validators/RegularExpressionValidator.cs @@ -45,6 +45,7 @@ namespace Spring.Validation #region Fields private string expression = string.Empty; + private bool allowPartialMatching = false; private RegexOptions options; #endregion @@ -98,6 +99,16 @@ namespace Spring.Validation set { expression = value; } } + /// + /// Gets or sets a value indicating whether to do a partial match instead of a full match. + /// Default is false. + /// + public bool AllowPartialMatching + { + get { return allowPartialMatching; } + set { allowPartialMatching = value; } + } + /// /// The for the regular expression evaluation. /// @@ -143,7 +154,14 @@ namespace Spring.Validation } Match match = Regex.Match(text, this.Expression, this.Options); - return match.Success; + if (allowPartialMatching) + { + return match.Success; + } + else + { + return match.Success && match.Index == 0 && match.Length == text.Length; + } } } } \ No newline at end of file diff --git a/test/Spring/Spring.Core.Tests/Validation/Validators/RegularExpressionValidatorTests.cs b/test/Spring/Spring.Core.Tests/Validation/Validators/RegularExpressionValidatorTests.cs index a0eaa20a..d5b2ab90 100644 --- a/test/Spring/Spring.Core.Tests/Validation/Validators/RegularExpressionValidatorTests.cs +++ b/test/Spring/Spring.Core.Tests/Validation/Validators/RegularExpressionValidatorTests.cs @@ -79,7 +79,7 @@ namespace Spring.Validation.Validators [Test] public void CaseSensitiveStringMatching() { - RegularExpressionValidator validator = new RegularExpressionValidator("ToString()", "true", @"^[A-Z]([a-z]{1})"); + RegularExpressionValidator validator = new RegularExpressionValidator("ToString()", "true", @"[A-Z][a-z]*"); Assert.IsTrue(validator.Validate("Aleksandar", new ValidationErrors())); Assert.IsFalse(validator.Validate("ALEKSANDAR", new ValidationErrors())); Assert.IsFalse(validator.Validate("aleksandar", new ValidationErrors())); @@ -119,13 +119,12 @@ namespace Spring.Validation.Validators } [Test] - public void Test() + public void AllowPartialMatching() { RegularExpressionValidator validator = new RegularExpressionValidator(); - validator.Expression = "^[A-Za-z]"; - Assert.True(validator.Validate("hello", new ValidationErrors())); - + validator.Expression = "[A-Za-z]"; + validator.AllowPartialMatching = true; + Assert.True(validator.Validate("123a456", new ValidationErrors())); } - } } \ No newline at end of file