0

I have tried everything I can find on here and all over the Internet and this just will not work.

I simply want to check the input type="number" to see if it matches a number already in the database. For now I have kept it simple as I can work out the rest.

Here is my code:

The Input:

 <input style="width: 50px; font-size: 17px;" type="number" min="1" autocomplete="off" id="days" name="days" value="1" onchange="daysChange(this.days)" required />

Here is the Javascript:

 function daysChange(days) {
     var day = document.getElementById("days");
     if (day == "3"){
         alert("You Already Have An Email Going Out That Day. Please Select Another Day.");
     }
 }

It's not picking up the value of days. Instead if I do an alert and have it output the value this is what it says... [object HTMLInputElement]

2
  • 1
    Try day.value. if (day.value == "3"){}. Commented Mar 10, 2017 at 23:04
  • day has the html element in it. You need to get the value in that element. as mentioned by the above comment Commented Mar 10, 2017 at 23:09

3 Answers 3

2

You need to get value:

function daysChange(days) {
 var day = document.getElementById("days");
 if (day.value == "3"){
 alert("You Already Have An Email Going Out That Day. Please Select Another Day.");
 }
 }
Sign up to request clarification or add additional context in comments.

Comments

0

As mentioned guys above, document.getElementById("days") returns the whole object of matched input. You need to get value attribute to get the current input value. Working example:

function daysChange(days) {
  var day = document.getElementById("days");
  
  if (day.value == "3"){
     alert("You Already Have An Email Going Out That Day. Please Select Another Day.");
  }
}
<input style="width: 50px; font-size: 17px;" type="number" min="1" autocomplete="off" id="days" name="days" value="1" onchange="daysChange(this.days)" required />

Comments

0

As mentioned before, you have to get value out of the HTMLInput:

if (day.value === "3")

Also good point, is that the value of the input is type string, I believe, you need type integer, so using a shorthand property for parseInt:

if (day.value|0 === 3)

Is much more pretty.

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.