Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

Date validation with ASP.NET validator

I'm trying to use an ASP.NET RangeValidator to validate a date on a textbox. The format of the date entered on the textbox is dd MMMM yyyy.

How can I use the range validator to validate a valid date? If I enter 1 January 1000 as the min or max value I get an error saying value cannot be converted to type date, but if I use another format it picks up my entered text as invalid.

Below is my code:

<asp:TextBox 
    runat="server" 
    ID="txtDatecompleted" 
/>
<cc2:CalendarExtender
    ID="datecompletedExtender" 
    runat="server"
    TargetControlID="txtDatecompleted"
    Format="dd MMMM yyyy"
/>  
<asp:RangeValidator 
    runat="server" 
    ID="RangeValidator1" 
    Type="Date" 
    ControlToValidate="txtDatecompleted" 
    MaximumValue="9999/12/28" 
    MinimumValue="1000/12/28" 
    ErrorMessage="enter valid date" 
    Display="None"
/>
<cc2:ValidatorCalloutExtender 
    ID="RangeValidator1_ValidatorCalloutExtender" 
    runat="server"
    Enabled="True"
    TargetControlID="RangeValidator1">
</cc2:ValidatorCalloutExtender>
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Best option would be

Add a compare validator to the web form. Set its controlToValidate. Set its Type property to Date. Set its operator property to DataTypeCheck eg:

<asp:CompareValidator
    id="dateValidator" runat="server" 
    Type="Date"
    Operator="DataTypeCheck"
    ControlToValidate="txtDatecompleted" 
    ErrorMessage="Please enter a valid date.">
</asp:CompareValidator>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...