Search code examples
jqueryreplacekeyup

jQuery replace day input to 00 if higher than 31


I am currently working on a age verification page and I need to make a jQuery script that will check if the date is correct, go to the next input and validate the user.

//Age Verification
$('.inp .day, .inp .month, .inp .year').on('keyup', function () {
    this.value = this.value.replace(/[^0-9\.]+/g, '');
});
$('.inp .day').on('keyup', function () {
    if (this.value >= 32) {
        this.value.replace(/[^0-9\.]+/g, '');
    } if (this.value.length >= 2) {
        $('.inp .month').focus();
    }

});
$('.inp .month').on('keyup', function () {
    if (this.value >= 13) {
        this.value.replace(/[^0-9\.]+/g, '');
    }
    if (this.value.length >= 2) {
        $('.inp .year').focus();
    }
});

This is what i have created and it works just fine except for the two if statements for day (if (this.value >= 32)) and month (if (this.value >= 13)).

Any help would be appreciated.

Thanks :)


Solution

  • shouldn't

    if (this.value >= 32) {
        this.value.replace(/[^0-9\.]+/g, '');
    

    be

    if (this.value >= 32) {
        this.value = 0;
    

    ?

    I don't see how the value >= 32 is being asked to do anything different than

    $('.inp .day, .inp .month, .inp .year').on('keyup', function () {
      this.value = this.value.replace(/[^0-9\.]+/g, '');
    });
    

    is already doing