Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript multiple fields validating

First, I have to validate that id and password textboxes are not empty(That one's working). Then I have to validate on the same form that id on textbox needs to be a number and also a number between 3000 and 3999 (That one doesn't work). Any ideas on what's wrong with my code?

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;
  }
}
like image 211
Monique Sullivan Avatar asked Jun 18 '26 21:06

Monique Sullivan


1 Answers

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;
  }
}
<!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>
like image 66
ankitkanojia Avatar answered Jun 20 '26 11:06

ankitkanojia