How do I verify age using jQuery?

后端 未结 8 2090
故里飘歌
故里飘歌 2020-12-04 04:28

I need to validate if the age for a alcohol website. And what I need is all here. I\'m nearly there but I\'m not sure is correct . Locally doesn\'t work. I need the

8条回答
  •  孤街浪徒
    2020-12-04 05:05

    Well, since you are going to have three input fields instead of one, the task is just to apply the mask to three textboxes instead of to one, and then constructing the argument to the timeago method call. The rest would be unchanged. Something like this:

    $('input[name="DAY"]').mask("99");
    $('input[name="MONTH"]').mask("99");
    $('input[name="YEAR"]').mask("9999");
    $('.deleteCookie').click(function() {
        $.cookie('age', null);
    });
    $('.submit').click(function() {
        var enteredDOB = $('input[name="DAY"]').val() + "/" +               
                         $('input[name="MONTH"]').val() + "/" +
                         $('input[name="YEAR"]').val();
        var age = jQuery.timeago(enteredDOB);
        if (age === 'NaN years ago') {
            alert('DOB must be valid');
        } else {
            $.cookie('age', age);
            if (parseInt(age, 10) >= 21) {
                alert('you\'re of age');
            } else {
                alert('you\'re too young');
            }
        }
    });
    
    if (null !== $.cookie('age')) {
        alert('saved Cookie ' + $.cookie('age'));
    }
    

    With regards to the "remember me" functionality, it is just a question of reading the age cookie upon page load, and redirecting to the next page if the age cookie contains a valid value, although this would probably be a check better done server-side.

提交回复
热议问题