仅限字母字符的输入框
Input box for Alpha Characters only
// input intials box
var createInput = document.createElement("input");
createInput.setAttribute("type", "text");
createInput.setAttribute("id", "initials");
// Max character length of 4 for people who have 2 middle names
createInput.setAttribute("maxlength", "4");
// Placeholder text
createInput.setAttribute("value", "ABC");
createInput.setAttribute("onkeydown", "return alphaOnly(event);");
// Input my initials JMS is value is blank
createInput.setAttribute(
"onblur",
"if (this.value == '') {this.value = 'JMS';}"
);
// Clear placeholder text onClick
createInput.setAttribute(
"onfocus",
"if (this.value == 'ABC') {this.value = '';}"
);
createInput.textContent = "";
questionsDiv.appendChild(createInput);
function alphaOnly(event) {
var key = event.keyCode;
return (key >= 65 && key <= 90) || key == 8;
}
因此,我使用 JavaScript 创建了一个只接受首字母的输入。输入框加载并且工作正常,但我的功能没有将键限制为 65-90 + 8,因为错误提示 alphaOnly 未定义。我错过了什么?
这将只使用字母
createInput.setAttribute(
"onkeypress",
"return ((event.charCode >= 65 && event.charCode <= 90) || (event.charCode >= 97 && event.charCode <= 122) || (event.charCode == 32))"
);
这限制了字符
createInput.setAttribute("maxlength", "4");
// input intials box
var createInput = document.createElement("input");
createInput.setAttribute("type", "text");
createInput.setAttribute("id", "initials");
// Max character length of 4 for people who have 2 middle names
createInput.setAttribute("maxlength", "4");
// Placeholder text
createInput.setAttribute("value", "ABC");
createInput.setAttribute("onkeydown", "return alphaOnly(event);");
// Input my initials JMS is value is blank
createInput.setAttribute(
"onblur",
"if (this.value == '') {this.value = 'JMS';}"
);
// Clear placeholder text onClick
createInput.setAttribute(
"onfocus",
"if (this.value == 'ABC') {this.value = '';}"
);
createInput.textContent = "";
questionsDiv.appendChild(createInput);
function alphaOnly(event) {
var key = event.keyCode;
return (key >= 65 && key <= 90) || key == 8;
}
因此,我使用 JavaScript 创建了一个只接受首字母的输入。输入框加载并且工作正常,但我的功能没有将键限制为 65-90 + 8,因为错误提示 alphaOnly 未定义。我错过了什么?
这将只使用字母
createInput.setAttribute(
"onkeypress",
"return ((event.charCode >= 65 && event.charCode <= 90) || (event.charCode >= 97 && event.charCode <= 122) || (event.charCode == 32))"
);
这限制了字符
createInput.setAttribute("maxlength", "4");