出生日期输入与JavaScript 13之后无法正常工作
我们正在进行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 ->
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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为错误是....
如果-------------------------------------------------------------------------------------------------------------------------------------------- -----------
------ 10---
-------------------------------------------------------------------------------------------------------------------------------
天
12
2000 - 年龄
,我认为错误是因为我认为您正在使用Selects标签。
选择标签具有选项标签。...每个选项标签都有一个值。
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.
So _select.value=13 is not posible.because there shouldn't be an
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...