简体   繁体   中英

8 is not less than or equal to 30?

Alright, I've got a RangeValidator :

<asp:RangeValidator ID="DateRangeValidator" runat="server" ControlToValidate="DateRange"
    ErrorMessage="The date range must be at least 1 day, not more than 30, and neither date can be greater than today's date."
    EnableClientScript="true" MinimumValue="1" MaximumValue="30" CssClass="errortext span9 offset2"
    Display="Dynamic" />

And as you can see the minimum is 1 and the maximum is 30.


That is validating a hidden field (it's visible at the moment cause I'm testing) :

<asp:TextBox ID="DateRange" runat="server" ClientIDMode="Static" />

And as you can see I've set the client ID to be static so it's finding the control just fine.


That hidden field is populated by this JavaScript method when one of the two dates change:

$('.datepicker').change(function () {
    var startDate = new Date($('#StartDate').val());
    var endDate = new Date($('#EndDate').val());

    if (startDate > Date() || endDate > Date()) {
        $('#DateRange').val(-1);
    }
    else {
        var nDifference = endDate - startDate;
        var one_day = 1000 * 60 * 60 * 24;
        $('#DateRange').val(Math.round(nDifference / one_day) + 1);
    }

    Page_ClientValidate(null);
});

And this method is working perfectly from the perspective of setting the correct number of days difference.


When the Page_ClientValidate is called I've debugged it to ensure the validator is firing as expected, and it is, and it has the values I would expect. When the minimum is checked it's grabbing 1 ... and when that is compared to a value of 8 ... it's evaluating as expected ... 8 is greater than or equal to 1.

However, when the maximum is checked, even though it's grabbing 30 for the maximum ... when it's compared to a value of 8 ... the expression that says 8 is less than or equal to 30 is being evaluated to false.


I really hope somebody can help me out here!

How in the world is 8 not less than 30 here?

您在RangeValidator上缺少Type="Integer"

Type="Integer"添加到范围验证器

You need to set the type on the RangeValidator :

<asp:RangeValidator ID="DateRangeValidator" runat="server" ControlToValidate="DateRange"
    ErrorMessage="The date range must be at least 1 day, not more than 30, and neither date can be greater than today's date."
    EnableClientScript="true" MinimumValue="1" MaximumValue="30" CssClass="errortext span9 offset2"
    Display="Dynamic"
    Type="Integer" />

Relevant MSDN docs: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basecomparevalidator.type.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM