【发布时间】:2020-01-16 20:17:18
【问题描述】:
首先,我必须验证 id 和密码文本框不为空(那个是有效的)。然后我必须在相同的表单上验证文本框上的 id 需要是一个数字,也是一个介于 3000 和 3999 之间的数字(那个不起作用)。关于我的代码有什么问题的任何想法?
function validatefunctions() {
if (document.getElementById('idtb').value === '') {
alert('You need to enter a Customer ID');
return false;
}
if (document.getElementById('pwtb').value === '') {
alert('Please enter your password');
return false;
}
var custID;
custID = document.getElementsByName("idtb").valueOf();
if (custID !== isNan) {
alert("Customer ID needs to be numeric");
return false;
}
if (custID < 3000) {
alert("ID must be above 3000");
return false;
}
if (custID > 3999) {
alert("ID must be below 3999");
return false;
}
}
【问题讨论】:
-
.valueOf();不正确。您可以使用.value来获取值(就像您在 yoruif语句中所做的那样)。要检查一个字符串是否不是一个数字,你可以使用isNaN(custID),当custID不是一个数字时它会返回true,当它是一个数字时false会返回true
标签: javascript html function validation