Why ASP. NET date data server verification fails

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.