简体   繁体   English

jQuery validate插件:如果任何特定条件无效,则重定向用户

[英]jQuery validate plugin: redirect user if any particular condition is invalidated

I am using 'jQuery Validate' plugin on a form which accepts birth-date as input from 3 separate fields for [month], [day] and [year]. 我在表格上使用“ jQuery Validate”插件,该表格接受出生日期作为来自[月],[天]和[年]的3个单独字段的输入。 I need to redirect the user to another url if his/her age is less than 21. I have looked for possible solutions over the internet and came up with a decision to handle the scenario with 'submitHandler' method of the plugin. 如果他/她的年龄小于21,我需要将用户重定向到另一个URL。我已经在互联网上寻找可能的解决方案,并想出了使用插件的'submitHandler'方法处理该场景的决定。

The new problem after adding submitHandler method along with my custom method, is that it has stopped validating other date ranges. 在与我的自定义方法一起添加了SubmitHandler方法之后,新的问题是它已停止验证其他日期范围。 Though it perfectly redirects the user when the age is calculated as less than 21. 尽管在计算年龄小于21岁时,它可以完美地重定向用户。

My code is as follows: 我的代码如下:

$.validator.addMethod("ageGateValidBirthdate", function (value, element) {
    var month = parseInt($('.age-gate-month').val(), 10);
    var day = parseInt($('.age-gate-day').val(), 10);
    var year = parseInt($('.age-gate-year').val(), 10);
    var userdate = new Date(year, month - 1, day);
    var CurrentDate = new Date();
    var flag = null;
    var validDate = false;
    var userDateInSeconds = userdate.getTime();
    var currentDateCompareVar = new Date();
    currentDateCompareVar.setTime(userDateInSeconds);
    if (CurrentDate > userdate && currentDateCompareVar.getFullYear() === year && currentDateCompareVar.getMonth() + 1 === month && currentDateCompareVar.getDate() === day) {
        validDate = true;
    }
    if (validDate && year > 1900) {
        flag = true;
        $('.age-gate-month, .age-gate-day, .age-gate-year').removeClass('error');
    }
    else {
        flag = false;
    }
    return flag;

}, "Enter correct birthdate");

$ageGatePage.on('submit', function (e) {
    e.preventDefault();
    jQuery.validator.addClassRules({
        "age-gate-month": {
            required: true,
            number: true,
            minlength: 1,
            maxlength: 2,
            range: [1, 12],
            ageGateValidBirthdate: true
        },
        "age-gate-day": {
            required: true,
            number: true,
            minlength: 1,
            maxlength: 2,
            range: [1, 31],
            ageGateValidBirthdate: true
        },
        "age-gate-year": {
            required: true,
            number: true,
            minlength: 4,
            maxlength: 4
        }
    });
    $ageGatePage.valid();
});

$ageGatePage.validate({
    groups: {
        dob: "month_of_birth day_of_birth year_of_birth"
    },
    messages: {
        //messages here
    },
    errorPlacement: function (error, element) {
        error.appendTo(element.parents(".age-gate-birth-date"));
    },
    submitHandler: function (form) {
        var month = parseInt($('.age-gate-month').val(), 10); // Convert to numbers with "+" prefix
        var day = parseInt($('.age-gate-day').val(), 10);
        var year = parseInt($('.age-gate-year').val(), 10);
        if (!(month > 0 && month < 13 && year > 0 && year < 32768 && day > 0 && day <= (new Date(year, month, 0))))
            return false;
        var userdate = new Date(year, month - 1, day); // Use the proper constructor
        var CurrentDate = new Date();
        var flag = null;
        var age = Math.floor((CurrentDate - userdate) / (365.25 * 24 * 60 * 60 * 1000));
        var ageGateFlag = age >= 21 ? true : false;
        if (ageGateFlag) {
            flag = true;
            form.submit();
        }
        else {
            flag = false;
            window.location.href = "http://domain.com/";
            return flag;
        }
        return flag;
    }
});

I just got the issue, posting it here for further reference. 我刚收到问题,将其发布在此处以供进一步参考。 It was the day validation inside the submitHandler which was creating the chaos. 正是当天提交者内部的验证造成了混乱。 Changing the code in following manner did the trick. 按照以下方式更改代码可以达到目的。

if (!(month > 0 && month < 13 && year > 0 && year < 32768 && day > 0 && day <= (new Date(year, month, 0))))
   return false;

to

if (!(month > 0 && month < 13 && year > 0 && year < 32768 && day > 0 && day <= 31))
   return false;

Tried some workaround with invalidHandler with no luck. 尝试了一些无效的invalidHandler解决方法。 If someone could post a better answer, it'd be really appreciable. 如果有人可以发布更好的答案,那将是非常可观的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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