[...] I was using that expression in
RegularExpressionValidator for asp.net page, as it is. It appears
regular expression OR "|" operator doesn't work in
RegularExpressionValidator control. So I changed the regular
expression to following equivalent:
Hi Anees,
At first, I thought that would be a bug in RegularExpressionValidator
control (since it is using the same System.Text.RegularExpressions.Regex
class, there is no way it couldn't handle "|" operator correctly), but
after checking through it source code, the problem is because the way it
handles matches (hence the problem with your regex pattern). Here is the
portion of the source code,
protected override bool EvaluateIsValid()
{
// Always succeeds if input is empty or value was not found
string controlValue = GetControlValidationValue(ControlToValidate);
Debug.Assert(controlValue != null, "Should have already been
checked");
if (controlValue == null || controlValue.Trim().Length== 0)
{
return true;
}
try
{
// we are looking for an exact match, not just a search hit
Match m = Regex.Match(controlValue, ValidationExpression);
return (m.Success && m.Index == 0 && m.Length ==
controlValue.Length);
}
catch
{
Debug.Fail("Regex error should have been caught in property
setter.");
return true;
}
}
Your regex pattern is
(\d+)|(\d+\s*[Xx]\s*\d+)
Since it use Regex.Match to get the match, when pass the input as
"100x100", Regex.Match is succeed (the matched value is "100"),
m.Success and m.Index == 0 returns true, but the m.Length ==
input.Length returns false which make the above EvaluateIsValid() method
returning false, hence the input failed the validation (which leads you
to think that the control does not handle "|" correctly).
If you modify your regex to include anchors, it would work just fine.
(^\d+$)|(^\d+\s*[Xx]\s*\d+$)
Regards.