年份格式需要在javascript中输入4位.subscript [英] Year format requires to enter 4 digits .subscript in javascript
问题描述
大家好
我有一个即将开展的项目。
我有一个验证程序要写,初学者的水平。
我编写了大部分代码,但是如果用户以2位数形式输入年份,我需要将其转换为4位数字表格。
然后我需要声明一个新变量并使用'.subscript'从国家识别号码中提取4位数字,这是一个13位数字。
我到目前为止编写了大部分代码(我不会在这里发布所有代码),但代码在达到yearString =时不会执行yearString.substring(2,4)。
基本上我需要声明的yearString变量并且工作4位数,即1987年。
显然它不起作用,因为1987 = [1,9,8,7]是一个4字符串数组, AS 我在索引4处没有字符串。
我希望我已经让自己明白了无知的奥利。我会留下来纠正。
Greets。
我尝试过:
Hi guys
I have an upcoming project.
I have a validation program to write, beginner's level.
I've written the code most part, but in case the user inputs the year in 2 digit form I need to convert it into 4-digit form.
Then I need to declare a new variable and extract using '.subscript' the 4-digits from the National Identification Number which is a 13 digit number.
I've written most of the code so far ( I won't post it all of it here), but the code won't execute when it reaches yearString = yearString.substring(2,4).
Basically I need the yearString variable declared and working for a 4 digit year, i.e. 1987.
Obviously it won't work, as 1987 = ["1", "9", "8", "7"] is a 4 string array, AS I don't have a string at index 4.
I hope I've made myself understood and sorry for the ignorance. I stay to be corrected.
Greets.
What I have tried:
function cnpVal() {
var cnpString = document.getElementById('lblcnp').value; //13 digit number
var dayString = document.getElementById('txtday').value;
var monthString = document.getElementById('txtmonth').value;
var yearString = document.getElementById('txtyear').value;
arrayCnp = cnpString.split('');
if (yearString.length != 4) {
alert("Year format requires you to enter 4 digits");
return false;
}
else {
yearString = yearString.substring(2,4);
}
推荐答案
显然它不起作用,因为1987 = [1,9,8,7]是一个4字符串数组,但我在索引4处没有字符串。
Obviously it won't work as 1987 = ["1", "9", "8", "7"] is a 4 string array, but I don't have a string at index 4.
显而易见!数组中的第一个元素位于索引0(零)。
JavaScript数组 [ ^ ]
JavaScript String substring()方法 [ ^ ]
这篇关于年份格式需要在javascript中输入4位.subscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!