Javascript 多字段验证

Mon*_*van 3 html javascript validation function

首先,我必须验证 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;
  }
}
Run Code Online (Sandbox Code Playgroud)

ank*_*jia 5

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 = document.getElementById('idtb').value;
  if (Number.isNaN(parseInt(custID))) {
    alert("Customer ID needs to be numeric");
    return false;
  }
  
  if (parseInt(custID) < 3000) {
    alert("ID must be above 3000");
    return false;
  }
  
  if (parseInt(custID) > 3999) {
    alert("ID must be below 3999");
    return false;
  }
}
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<body>
<form  action="#" onsubmit="return validatefunctions()" method="post">

  Customer ID: <input type="text" name="idtb" id="idtb"><br /><br />

  Password: <input type="text" name="pwtb" id="pwtb"><br /><br />

  <input type="submit" value="Submit">

</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)