Mvc 2.0 Preview2 adds the client-side verification function. However, some colleagues found that the client-side verification was successful when the regular expression was used to verify the Date input, but the server-side verification always failed.
Check the code of a colleague and no obvious problems are found: (Note: here the regular expression is simplified)
[RegularExpression ("^ 20 \ d-\ d? \ D-\ d $ ")]
Object somedate {get; set ;}
Run. Enter 2009-12-30 in the text box. The server prompts that the regular expression fails to match.
Puzzled. decompile RegularExpressionAttribute. The IsValidate code is as follows:
Public override bool IsValid (object value)
{
String str = Convert. ToString (value, CultureInfo. CurrentCulture );
If (string. IsNullOrEmpty (str ))
{
Return true;
}
Match match = this. Regex. Match (str );
Return (match. Success & (match. Index = 0) & (match. Length = str. Length ));
}
The problem was finally found. Originally, after the date data was Convert. ToString, the generated date was time-based, for example, 0:00:00, so regular expression matching always failed. Therefore, when matching a date, you should set aside the time data location for the regular expression. In this regard, both WebForm and Mvc are consistent.