jQuery验证 [英] JQuery Validation

查看:58
本文介绍了jQuery验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证我的表单...因此基本上可以正常工作,但是有点太好了,我有两个文本框,一个是开始日期,另一个是结束日期,格式为mm/dd/yyyy

Im trying to get my form to validate...so basically its working, but a little bit too well, I have two text boxes, one is a start date, the other an end date in the format of mm/dd/yyyy

如果开始日期大于结束日期...存在错误 如果结束日期小于开始日期...则存在错误 如果开始日期小于今天的日期...存在错误

if the start date is greater than the end date...there is an error if the end date is less than the start date...there is an error if the start date is less than today's date...there is an error

唯一的是当我更正错误时,错误警告仍然存在...这是我的代码:

The only thing is when I correct the error, the error warning is still there...here is my code:

    dates.change(function () {
        var testDate = $(this).val();
        var otherDate = dates.not(this).val();
        var now = new Date();
        now.setHours(0, 0, 0, 0);

        // Pass Dates
        if (testDate != '' && new Date(testDate) < now) {
            addError($(this));
            $('.flightDateError').text('* Dates cannot be earlier than today.');
            isValid = false;
            return;
        }

        // Required Text 
        if ($(this).hasClass("FromCal") && testDate == '') {
            addError($(this));
            $('.flightDateError').text('* Required');
            isValid = false;
            return;
        }
        // Validate Date
        if (!isValidDate(testDate)) {
            // $(this).addClass('validation_error_input');  
            addError($(this));
            $('.flightDateError').text('* Invalid Date');
            isValid = false;
            return;
        }
        else {
            // $(this).removeClass('validation_error_input');
            removeError($(this));
            if (!dates.not(this).hasClass('validation_error_input'))
                $('.flightDateError').text(' ');
        }

        // Validate Date Ranges
        if ($(this).val() != '' && dates.not(this).val != '') {
            if ($(this).hasClass("FromCal")) {
                if (new Date(testDate) > new Date(otherDate)) {
                    addError($(this));
                    $('.flightDateError').text('* Start date must be earlier than end date.');
                    isValid = false;
                    return;
                }
            }
            else{
                if (new Date(testDate) < new Date(otherDate)) {
                    addError($(this));
                    $('.flightDateError').text('* End date must be later than start date.');
                    return;
                }
            }
        }
    });

我相信主要问题是这部分

The main Issue is this part, I believe

 // Validate Date Ranges
            if ($(this).val() != '' && dates.not(this).val != '') {
                if ($(this).hasClass("FromCal")) {
                    if (new Date(testDate) > new Date(otherDate)) {
                        addError($(this));
                        $('.flightDateError').text('* Start date must be earlier than end date.');
                        isValid = false;
                        return;
                    }
                }
                else{
                    if (new Date(testDate) < new Date(otherDate)) {
                        addError($(this));
                        $('.flightDateError').text('* End date must be later than start date.');
                        return;
                    }
                }
            }

testDate是开始日期
otherDate是结束日期

testDate is the start date
otherDate is the end date

这是两个输入框所在的C#

Here is the C# where the two input boxes are

    <div id="campaign_start" style="display: inline-block">
                    <label class="date_range_label">from:</label>  
                    <asp:TextBox ID="FromCalTbx" runat="server" Width="100px" CssClass="FromCal editable float_left required" />
                  </div>
                  <div id="campaign_end" style="display: inline-block">
                    <label class="date_range_label">to:</label>
                    <asp:TextBox ID="ToCalTbx" runat="server" Width="100px" CssClass="float_left optional"/>
</div>

推荐答案

您正在使用相同的方法测试两个日期,但是需要不同的结果.看起来"FromCal"类在这里非常重要;您可以显示一些HTML使其更清楚地显示其功能以及日期选择器如何工作吗? (对不起,我的声誉阻止了我发表评论)

You are testing both dates with the same method, but need different outcomes. Looks like the 'FromCal' class is pretty important here; can you show some HTML to make it more clear what it does and how the date picker works? (Sorry about the reply, my reputation prevents me from commenting, I think)

这篇关于jQuery验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆