jQuery 如果大于 31,则将日输入替换为 00

jQuery replace day input to 00 if higher than 31

我目前正在开发年龄验证页面,我需要制作一个 jQuery 脚本来检查日期是否正确,转到下一个输入并验证用户。

//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();
    }
});

这是我创建的,它工作正常,除了天 (if (this.value >= 32)) 和月 (if (this.value >= 13)) 的两个 if 语句。

如有任何帮助,我们将不胜感激。

谢谢:)

不应该

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

成为

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

?

我不明白如何要求值 >= 32 执行与

不同的操作
$('.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 > 31)
    {
        this.value = "00";
    }
    else if (this.value.length >= 2) {
        $('.inp .month').focus();
    }
});

尝试

//Age Verification
$('.inp .day, .inp .month, .inp .year').on('keyup', function () {
    var $this = $this,
        thisVal = $this.val().replace(/[^0-9\.]+/g, '');
    $this.val(thisVal);
});
$('.inp .day').on('keyup', function () {
    var $this = $this,
        thisVal = $this.val();
    if (thisVal >= 32) {
        $this.val('');// or whatever it should be...31?
    }else if (thisVal.length >= 2) {
        $('.inp .month').focus();
    }

});
$('.inp .month').on('keyup', function () {
    var $this = $this,
        thisVal = $this.val();
    if (thisVal >= 13) {
        $this.val('');// or whatever it should be...12?
    }else if (this.value.length >= 2) {
        $('.inp .year').focus();
    }
});

如果您希望在用户键入无效数据时该值为空,那么为什么不这样做:

$('.inp .day, .inp .month, .inp .year').on('keyup', function () {
    this.value = ""; //Make it a blank string
});
$('.inp .day').on('keyup', function () {
    if (this.value >= 32) {
        this.value = ""; //Make it a blank string
    } if (this.value.length >= 2) {
        $('.inp .month').focus();
    }
});
$('.inp .month').on('keyup', function () {
    if (this.value >= 13) {
        this.value = "";//Make it a blank string
    }
    if (this.value.length >= 2) {
        $('.inp .year').focus();
    }
});