Why RegularExpressionAttribute
validation doesn't compare the input string with the value of all matches concatenated?
I asked a question here about the scenario below, but I found a solution the next day and found it better to raise the issue here.
[Required(
AllowEmptyStrings = false,
ErrorMessage = "Required")]
[RegularExpression(
"^[^0]{1}|..+",
ErrorMessage = "Expressao Regular")]
public string EncryptedValue { get; set; }
Except by empty string or "0", the property should be valid in ModelState, but:
You can test HERE the expression and value.
Expression
^[^0]{1}|..+
Value
+iCMEBYZQtWbnU2RPX/MmqrDPuVJzSGGWhkFd+9/zpMbHVoOlZFuF9ND1xAxsQy3YFCPIsUBEgg2RJNkPefrmQ==
You will notice that the expression match, but with two matches. The first match itself are not equals to the input string, you need to concatenate both match value to reach that.
But apparently this is not done in the validation of ModelState, even with jquery.validate.unobtrusive this happens (with jquery, I need to click in submit button two times to see this, but it's happens).
Solution
You need to build an expression that match input string completelly in the first match.
When you build a expression to validate a field, every OR in your expression must match all the input string.
So whenever you mount an expression with OR operators, always mount from largest input to smallest input.
In this case:
From ^[^0]{1}|..+
to ..+|^[^0]{1}