如何制作<imput>
识别字母和数字并忽略空格或大写拼写的字段?
下面是我到目前为止所做的,我的目标是feedback
是 "right"
当9 H 6 U 8 f
被写入字段中,即使字符之间没有空格或者字母不是大写。
function F1() {
antw = parseFloat(document.getElementById("userAnswer").text);
feedBack = document.getElementById("feedBack");
ant = document.getElementById("answer").textContent;
{
if (antw == ant) {
feedBack.textContent = "Right";
} else {
feedBack.textContent = "Wrong";
}
}
}
<button onclick="F1()">check</button>
<input id="userAnswer" type=text>
<p id="feedBack"> </p>
Write:<label id="answer"> 9 H 6 U 8 f</label>
最佳答案
您需要使用删除两个值中的所有空格
.replace(/\s+/g, '')
然后使用将两者转换为小写
.toLowerCase()
然后您可以独立于大小写和空格来比较它们:
function F1() {
feedBack = document.getElementById("feedBack");
antw = document.getElementById("userAnswer").value.replace(/\s+/g, '').toLowerCase();
ant = document.getElementById("answer").textContent.replace(/\s+/g, '').toLowerCase();
{
if (antw == ant) {
feedBack.textContent = "Right";
} else {
feedBack.textContent = "Wrong";
}
}
}
<button onclick="F1()">check</button>
<input id="userAnswer" type="text">
<p id="feedBack"> </p>
Write:<label id="answer"> 9 H 6 U 8 f</label>
请注意,要获取 input
元素的内容,请使用 .value
,而不是 .text
。
关于javascript - 如何用Javascript制作一个识别字母和数字的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61165324/