0

Ok so i am making a website which converts kilometer to meter and i take the input when a button is clicked and i take the input with the help of javascript my code is working but in the input box you can even write alphabets which i don't want i want the input box to just accept numeric letters my code

document.getElementById("button1").addEventListener("click",function ktm(){
    let ktm1=window. prompt("Enter kilometer's value: ");
    alert(ktm1+" kilometer in meters would be "+ktm1*1000+" meters") ;
})
7
  • 2
    nothing you can do about it in a prompt - use an <input type="number" along with code that actively prevents you from even pasting non numbers into it Commented Aug 18, 2021 at 7:43
  • 2
    With prompt, all you can do is validate the input after the fact and prompt the user again if the input was not numeric. Commented Aug 18, 2021 at 7:44
  • bravo I want it to popup when clicked i also know that I can do it that way but it would be messy Commented Aug 18, 2021 at 7:45
  • deceze can you give the code to prompt the user again if the input was not numeric. Commented Aug 18, 2021 at 7:50
  • @MuhammadJamil You could try to make a input type=number and then use that as the input for your JS. Then just use document.getElementById("TotalKilometer").value on the input. Commented Aug 18, 2021 at 7:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.