出生日期输入与JavaScript 13之后无法正常工作

发布于 2025-02-01 20:15:41 字数 2040 浏览 2 评论 0原文

我们正在进行AB测试。我们只能通过Google Optimize触摸客户端代码。因此,设计师要求我们将出生日期格式从分离的输入更改为这样的统一输入 - >

因此,想法是用户键入生日数字[13.07.1998],然后这些信息将到达分离的输入,例如This [13] [7月] [1998]

它运行正常,直到我们使用第13天。在该数字之后,代码不起作用。但是,如果我使用12,则有效。

const dobInput = document.querySelector("input");
const birthDay = document.querySelector("#birthDay");
const birthMonth = document.querySelector("#birthMonth");
const birthYear = document.querySelector("#birthYear");

dobInput.addEventListener("change", (e) => {
  const dobString = e.target.value;

  if ( isValidDate(dobString) ) {

    let date = dobString.slice(0, 2);
    let month = dobString.slice(3, 5);
    let year = dobString.slice(6, 10);

    birthDay.value = date;
    birthMonth.value = month;
    birthYear.value = year;

  } else {

    const errorMessage = document.querySelector(".cro-error-text");
    errorMessage.classList.add('error');
    
  }
});

function isValidDate(dateString) {
  // First check for the pattern
  if (!/^\d{1,2}\.\d{1,2}\.\d{4}$/.test(dateString)) return false;

  // Parse the date parts to integers
  var parts = dateString.split(".");
  var day = parseInt(parts[1], 10);
  var month = parseInt(parts[0], 10);
  var year = parseInt(parts[2], 10);

  // Check the ranges of month and year
  if (year < 1905 || year > 2004 || month == 0 || month > 12) return false;

  var monthLength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

  // Adjust for leap years
  if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
    monthLength[1] = 29;

  // Check the range of the day
  return day > 0 && day <= monthLength[month - 1];
}

有人知道为什么会发生这种情况吗?谢谢!

We are doing AB testing. We can only touch the client side code with Google Optimize. So the designer asked us to change the birth date format from separated input to an unify input like this ->

enter image description here

So the idea is that the user type the birth date numbers [13.07.1998] and then the information will get to the separated input like this [13] [July] [1998]

It works fine until we use day 13. After that number the code does not work. But if I use 12 it works.

enter image description here

const dobInput = document.querySelector("input");
const birthDay = document.querySelector("#birthDay");
const birthMonth = document.querySelector("#birthMonth");
const birthYear = document.querySelector("#birthYear");

dobInput.addEventListener("change", (e) => {
  const dobString = e.target.value;

  if ( isValidDate(dobString) ) {

    let date = dobString.slice(0, 2);
    let month = dobString.slice(3, 5);
    let year = dobString.slice(6, 10);

    birthDay.value = date;
    birthMonth.value = month;
    birthYear.value = year;

  } else {

    const errorMessage = document.querySelector(".cro-error-text");
    errorMessage.classList.add('error');
    
  }
});

function isValidDate(dateString) {
  // First check for the pattern
  if (!/^\d{1,2}\.\d{1,2}\.\d{4}$/.test(dateString)) return false;

  // Parse the date parts to integers
  var parts = dateString.split(".");
  var day = parseInt(parts[1], 10);
  var month = parseInt(parts[0], 10);
  var year = parseInt(parts[2], 10);

  // Check the ranges of month and year
  if (year < 1905 || year > 2004 || month == 0 || month > 12) return false;

  var monthLength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

  // Adjust for leap years
  if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
    monthLength[1] = 29;

  // Check the range of the day
  return day > 0 && day <= monthLength[month - 1];
}

Does anyone knows why it could be happening? thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛装女皇 2025-02-08 20:15:41

我认为错误是....

如果-------------------------------------------------------------------------------------------------------------------------------------------- -----------

------ 10---

-------------------------------------------------------------------------------------------------------------------------------

12

2000 - 年龄



,我认为错误是因为我认为您正在使用Selects标签。

选择标签具有选项标签。...每个选项标签都有一个值。

<select>
<option value=1>Jenuary</option>
<option value=2>February</option>
</select>

13不适合

<option value=13>


所以_select.Value = 在选择标签中...

I think the error is this....

if-----------------------------------------

12 - - - 10 - - - 2000

Day - - - month - - - year

in your code you are saving

12 - - - - 10 - - - 2000

Month - - -Day - - - -year



I think the error is this because I think you are using selects tag.

select tags have options tag....and each option tag have a value.

<select>
<option value=1>Jenuary</option>
<option value=2>February</option>
</select>

So _select.value=13 is not posible.because there shouldn't be an

<option value=13>

There is not a month 13...I think you are confusing month with day when you store it in the variable


Or check if all options tags are within select tag...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文