3

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;
  }
}
2
  • 1
    .valueOf(); is incorrect. You can use .value instead to get the value (like you've done in yoru if statements). To check if a string is not a number you can use isNaN(custID), which will give true when the custID is not a number, and false when it is a number Commented Sep 16, 2019 at 1:10
  • developer.mozilla.org/en-US/docs/Learn/HTML/Forms/… Commented Sep 16, 2019 at 1:43

2 Answers 2

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;
  }
}
<!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>

Sign up to request clarification or add additional context in comments.

1 Comment

You're code is near to correct but you should use Number function of inbuilt javascript function which will return the data-type of value. so you just need to use Number.isNaN function rather than document.getElementsByName("idtb").valueOf() in your code./
0

textbox needs to be a number and also a number between 3000 and 3999 (That one doesn't work)

Why don't use input type number specifying the min and max attribute:

<form>
  <input type="number" name="quantity" min="3000" max="3999" value="3000">
  <input type="submit">
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.