0

i am quite new to JavaScript. I am trying to make it so that if anything other than a number is entered it either blocks it or it takes their entry and tells them to enter a number (because their entry is not acceptable) - I don't know which is easier.

The code I have so far:

const calculateEntered = document.querySelector(".js- 
    calculateButton ")

    const calculateTotal = () => {

      let enteredValue = document.querySelector(".js-enterValue").value


      if (enteredValue < 40) {
        console.log(`<40>`)
        enteredValue = parseFloat(enteredValue) + 10
        document.querySelector(".js-totalText").innerHTML = `Your 
    total is: £${enteredValue}`
      } else if (enteredValue => 40 && enteredValue != NaN) {


        document.querySelector(".js-totalText").innerHTML = `Your total 
    is: £${enteredValue}`
      }
    }
<button class="js-calculateButton">Calculate total</button>
<div>
  <div>
    <p class="js - enterTotalHere">Enter order total here: £</p>
  </div>
  <div><input placeholder="cost of order" class="js- 
    enterValue"></input>
  </div>
</div>
<p class="js-totalText"> Your total is: </p>

Any characters added lead to the "else if" statement. I tried looking this up but the solutions seemed very difficult - I didn't want to use any 3rd party method.

9
  • Why not use type="number"? Commented Jun 16, 2023 at 16:50
  • 2
    If you use an input element, you can set it to type="number" and the browser will display a message if the value that's entered is not a number. Commented Jun 16, 2023 at 16:50
  • 2
    Also, why are there spaces here? class="js - enterTotalHere" Commented Jun 16, 2023 at 16:53
  • 1
    </input> closing tag doesn't exist. Your code is invalid Commented Jun 16, 2023 at 16:53
  • Specically, to your case type="number" can be used with min="0" to set a minimum value, zero here as it is a cost, it won't be negative Commented Jun 16, 2023 at 16:54

1 Answer 1

0

You can listen for the input event on the input element and then calculate the result.

document.forms.form01.cost.addEventListener('input', e => {
  let form = e.target.form;
  let costNum = Number(e.target.value);
  form.result.value = (costNum < 40) ? costNum + 10 : costNum;
});
<form name="form01">
  <button class="js-calculateButton">Calculate total</button>
  <p class="js - enterTotalHere">Enter order total here: £</p>
  <div>
    <input name="cost" type="number" placeholder="cost of order" class="js-enterValue">
  </div>
  <p class="js-totalText"> Your total is: £<output name="result"></output></p>
</form>

If you don't like the arrows on the input element (type="number") an alternative could be to use type="text" inputmode="numeric" pattern="\d*":

document.forms.form01.cost.addEventListener('input', e => {
  let form = e.target.form;
  let costNum = Number(e.target.value);
  form.result.value = (costNum < 40) ? costNum + 10 : costNum;
});
<form name="form01">
  <button class="js-calculateButton">Calculate total</button>
  <p class="js - enterTotalHere">Enter order total here: £</p>
  <div>
    <input name="cost" type="text" inputmode="numeric" pattern="\d*" placeholder="cost of order" class="js-enterValue">
  </div>
  <p class="js-totalText"> Your total is: £<output name="result"></output></p>
</form>

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

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.